if statement based on serial string

edited September 2014 in Questions about Code

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);

}
Tagged:

Answers

This discussion has been closed.