I had same issue as original poster. I am sending to Processing both a variety of Sensor values to be used by Objects like meter, or Text to display on the canvas. I am also ending simple Arduino debug messages that can be displayed in the console that are not used by any Processing object. So that my Processing code can distinguish these apart and filter the data to be sent to the various canvas objects I will add identifying characters in front of the sensor data. For example a sensor value meant for meter object m1 may have appended in front of the value “v1m*”, so the arduino would send via Serial.println(“v1m123”); if the value where say 123. Then using the various string functions parse out the integer data and assign the int() value of that to the meter instance m1.
I am sure there is a more elegant way to do this but this is what I have come up with.
In order to get the int() function to work I had to remove 2 characters from the received data and I do not understand why when I expected it to be only one.
Please help me to understand why?
import processing.serial.;
import meter.;
Meter m;
int sensorValue;
Serial myPort; // The serial port
int whichKey = -1; // Variable to hold keystoke values
int inByte = -1; // Incoming serial data
String Adata = “0”;
String debugMsg;
String deviceData;
String astr = “*”;
void setup() {
size(800, 600);
m = new Meter(this, 50, 175, false);
m.setUp(0, 212, 0.0, 212.0, 180.0, 360.0);
String[] scaleLabels = {“0”, “20”, “40”, “60”, “80”, “100”, “120”, “140”, “160”, “180”, “200”, “220”};
m.setScaleLabels(scaleLabels);
m.setDisplayDigitalMeterValue(true);
m.setTitleFontColor(color(0, 0, 255));
m.setTitle(“Temperature”);
// create a font with the third font available to the system:
PFont myFont = createFont(PFont.list()[2], 14);
textFont(myFont);
// List all the available serial ports:
printArray(Serial.list());
String portName = Serial.list()[Serial.list().length - 1];
myPort = new Serial(this, portName, 57600);
myPort.clear();
myPort.bufferUntil(’\n’); //Sets # bytes to buffer before calling serialEvent()
}
void draw() {
background(55);
//text("Debug: " + debugMsg, 10, 160);
text("Adata Received: " + Adata, 10, 130);
text("Last Sent: " + (char)whichKey, 10, 100);
// Input for testing.
// Update the sensor value to the meter.
m.updateMeter(sensorValue);
// Use a delay to see the changes.
delay(700);
}
void serialEvent(Serial myPort) {
String delim = “v1m”;
int l1;
if(myPort.available() > 0) {
//println(deviceData);
deviceData = myPort.readStringUntil('\n');
//The * must be the first char in the received String so we know we have a string meant for
//Processing, otherwise it is a debug
//statement to just display in the console or in a TEXT object.
if(deviceData.indexOf(delim) == 0) {
l1 = deviceData.length();
Adata = deviceData.substring(3, l1-2); //Extract only the numerical values, I do not know why I
//have to subtract 2 characters from the length instead of just 1 in order to get int() to work?????
println(Adata);
sensorValue = int(Adata);
println(sensorValue);
}
else
{
debugMsg = deviceData;
print(debugMsg);
}
}
//sendData(“Sending data to host\n”);
}
void keyPressed() {
// Send the keystroke out:
myPort.write(key);
whichKey = key;
myPort.write(’\n’);
//delay(5000);
}
void sendData(String Sdata) {
myPort.write(Sdata);
}