Help Parsing Serial Data
in
Integration and Hardware
•
7 months ago
I am trying to get a Processing program to work with an Arduino Uno with three potentiometers hooked up to the Arduino board.
The Arduino program I have so far seems to print to the serial port the correctly-formatted values, such as:
15" MacBook Pro, 2.4GHz Intel Core i5, OS X v.10.2.8 Mountain Lion,
QuickTime Player v.10.2, Processing v. 1.5.1 and v.2.0b8, Arduino Uno, Nano v.3.
123, 180, 200*123, 180, 200* etc.
Here is that code:
// Arduinno Code Not Processing code=========
int sensor1 = 0;
int sensor2 = 0;
int sensor3 = 0;
void setup(){
Serial.begin (9600);
pinMode (3, INPUT);
}
void loop(){
if (Serial.available() > 0){
Serial.read();
sensor1 = analogRead(A0);
sensor2 = analogRead(A1);
sensor3 = analogRead(A2);
Serial.print (sensor1, DEC);
Serial.print (',');
Serial.print (sensor2, DEC);
Serial.print (',');
Serial.print (sensor3, DEC);
Serial.print ('*');
}
}
// end Arduino code ===================================
// end Arduino code ===================================
My problem is that my Processing code only works intermittently. Sometimes when I start it, it works fine, but mostly it shuts off after putting up the sketch window. I suspect there must be something wrong with my method of taking in and handling the stream of serial data coming in from the Arduino.
Here's the Processing code v 2.0b8 of Processing:
import processing.serial.*;
int r, g, b;
Serial port;
void setup() {
size(200, 200);
println(Serial.list());
port = new Serial(this, Serial.list()[4], 9600);
port.write(65);
}
void draw() {
background(r, g, b);
}
void serialEvent(Serial port) {
String input = port.readStringUntil('*');
if ( input != null) {
//println("Receiving: " + input);
int[] vals = int(splitTokens(input, ",*"));
r = vals[0];
g = vals[1];
b = vals[2];
r = r/4;
g = g/4;
b = b/4;
println(r + ", " + g + ", " + b);
}
port.write(65);
}
These examples were derived from Daniel Shiffman's code on pp. 376-7 in
Learning Processing.
I have tried adding a delay(xx); function to the Arduino code, but that hasn't helped. I'm still getting intermittent performance.
Any help or suggestions would be greatly appreciated.
Many thanks,
George
QuickTime Player v.10.2, Processing v. 1.5.1 and v.2.0b8, Arduino Uno, Nano v.3.
1