how to perform a conditional check in processing on a value from serial read from arduino

edited January 2018 in Arduino

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() {
  pinMode(pushButton, INPUT);

void loop() {
  int buttonState = digitalRead(pushButton);
  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(){
  String portName = Serial.list()[2];
  myPort = new Serial(this,portName,9600);

void draw(){

void serialEvent(Serial myPort){
  String inputString = myPort.readStringUntil(10); //until newline or ("\n");
  inputString = trim(inputString);
  if (inputString == "1"){ //this doesn't work, even though println will render ("1") plus the newline if button is pushed.
  } else {


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.


Sign In or Register to comment.