If statement with string from Arduino

edited June 2014 in Questions about Code

Hey everybody, my first post here :)

So, I have hooked up my arduino which sends out two different strings depending on the condition of a tilt sensor to the serial port. These two strings are then successfully read and printed out in the console whenever I tilt my arduino, however I have an IF statement that wants to check which string is currently being printed and then do an action. Unfortunately, It won't happen and I would appreciate any kind of help!


import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port usbmodem1451

String str1 = "PLAY\n";

void setup(){
String portName = Serial.list()[2];
myPort = new Serial(this, portName, 9600);

void draw()

  if ( myPort.available() > 2) 

  val = myPort.readStringUntil('\n');   // read it and store it in val
    if (val.equals("PLAY\n") == true){

// I have also tried 
//(val.equals("PLAY") == true);
//(val.equals(str1) == true);

else {

  println(val); //print it out in the console



  • if (val.trim().equals("PLAY")) { // == true is not necessary / useful

    trim() removes whitespace, including newlines. Not guaranteed to work, worth a try... :)

Sign In or Register to comment.