I have a simple push button connected to pin 2 on my Arduino. I can read this value and print it to the serial monitor without any trouble:
int pushButton = 2;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
int buttonState = digitalRead(pushButton);
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
This prints a value of 0 when the button is not pressed, and 1 if it is.
In Processing, I want to check if the value is zero or one, and perform some conditional logic. But I can't get the equality right:
Serial myPort;
String resultString;
void setup(){
size(640,480);
printArray(Serial.list());
String portName = Serial.list()[2];
myPort = new Serial(this,portName,9600);
myPort.bufferUntil(10);
}
void draw() {
//
}
void serialEvent(Serial myPort){
String inputString = myPort.readStringUntil(10); //until newline or ("\n");
inputString = trim(inputString);
println(inputString);
if (inputString == "1"){ //this doesn't work, even though println will render ("1") plus the newline if button is pushed.
println("on");
} else {
println("off");
}
}
How do I set up this conditional to do one thing if the button is pushed, and another if it is not?
I've tried converting the string to an int using
inputInt = Integer.parseInt(inputString);
and then performing the check, but it didn't work.