We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm using Processing to do something depending on the state of an input (eg, button, sensor, potentiometer).
Having wired a switch via an Arduino I pickup the serial data via Processing and have that data displayed on screen.
There is an if statement that I can't get working. Is should draw a green ellipse if the swicth is flicked (reading of 1023), otherwise ir will draw a red ellipse.
I can't figure out the if statement.
Can anyone advise what I should be doing to have the condition work? You can see where I'm going wrong (line 28) where is says "if (sensorReading == "1023")"
import processing.serial.*;
Serial myPort;
String sensorReading="";
PFont font;
void setup() {
size(400,200);
myPort = new Serial(this, "COM6", 9600);
myPort.bufferUntil('\n');
font = createFont(PFont.list()[2],32);
textFont(font);
}
void draw() {
//The serialEvent controls the display
}
void serialEvent (Serial myPort){
sensorReading = myPort.readStringUntil('\n');
if(sensorReading != null){
sensorReading=trim(sensorReading);
}
writeText("Sensor Reading: " + sensorReading);
if (sensorReading == "1023"){
fill(0,255,0);
ellipse(10,10,50,50);
}
else{
fill(255,0,0);
ellipse(10,10,50,50);
}
}
void writeText(String textToWrite){
background(255);
fill(0);
text(textToWrite, width/20, height/2);
}
Answers
Search String in the Reference page. Look what they have to say about using == to compare strings!
In short, don't use ==, use .equals() instead.
http://processing.org/reference/String_equals_.html