We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have an Arduino sending serial.print data whenever a button is used. The strings are things such as "3High", "3Low", "4High", "4Low", etc...
I want to use a series of if statements to trigger events via processing. For example, when "3High" is read, something will happen. When a combination are pressed (eg, "3Low" + "4Low") something else will happen.
I'm struggling with the syntax of the if statement...
Here's some code I'm experimenting with, an ellipse should appear when switch 3 is pressed:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
void setup()
{
// Open whatever port is the one you're using.
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readStringUntil('\n'); // read it and store it in val
}
println(val); //print it out in the console
if (val == "3Low"){
ellipse(50, 50, 100, 100);
}
//DRAW RECTANGLE (that changes on different switch states)
background(200);
}
Answers
Don't use == to compare Strings. Use the equals() method instead.
Here is a good reference explaining why: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java
Why another thread for the very same issue here?
http://forum.processing.org/two/discussion/7293/using-arduino-buttons-to-do-something-in-processing-best-method
Gonna lock this 1 up! (~~)