Checking Boolean variable breaks serial communication code with Arduino

I’m learning how to use processing to communicate via serial with arduino. Using the basic code below works, both variables are getting written to the arduino. And they then get printed to the processing console. (serialConnection is a custom class which identifies the port on which the arduino is connected).

  if (serialConnection.isReady) {
      var_id = 1;
      var_val = 127;
      serialConnection.serialPort.write(var_id);
      serialConnection.serialPort.write(var_val);
    //  println(1);
    }

    delay(50);
    while (serialConnection.serialPort.available() > 0) {
      int inByte = serialConnection.serialPort.read();
      println(inByte);
    //  println(3);
    }
  }

Arduino code:

void loop() {
  if (Serial.available()) {
    for (int i = 0; i < 2; i++) {
      serial_array [i] = Serial.read();
    }

    delay(50);
    Serial.write(serial_array [1]);
    // Serial.print(var);
  }
}

Changing the code to check a Boolean variable (so the array in the arduino only gets written to once per cycle) causes the while serial available statement to never execute. I cannot figure out why. No other changes made to the code.

  if (serialConnection.isReady) {
    if (data_processed == false) {
      var_id = 1;
      var_val = 127;
      serialConnection.serialPort.write(var_id);
      serialConnection.serialPort.write(var_val);
      data_processed = true;
      println(1);
    }

    delay(50);
    while (serialConnection.serialPort.available() > 0) {
      int inByte = serialConnection.serialPort.read();
      println(inByte);
      data_processed = false;
      println(3);
    }
  }

I would appreciate any guidance for what could be going wrong.

Hi,

Logically it means that the condition serialConnection.serialPort.available() > 0 is evaluated to false. Does the first if block execute and prints 1? Where is data_processed coming from I suppose it’s false by default?

Hi Joseph, apologies for the delay in responding. Yes, the first if block executes. The data_processed is false by default, should have mentioned. And yes, the 2nd block does not execute. It does however execute, if I’m not making this check against the Boolean variable, which is what is confusing. Am I making some error in the code, or is it perhaps it’s a timing issue with serial of some sort?

Hi @grudovz. It’s very easy to make a serial comms arrangement that appears to be correct but locks up if something varies. The ‘something’ might be that the Arduino is not ready (because it resets when Processing connects) or Processing hangs for a moment while the O.S. is busy. Where you test for ‘available’ you are not waiting to ensure all the characters have arrived (yes, think you are only using 2). You are transmitting an id and a value, but nothing to identify which is which. With that scheme I’ve always wondered if it can be relied on that everything stays in step. I think you are using binary data, which is alright, but ascii (text) is much easier to work with because you can print it directly.

Some months ago I made an example which tolerates all the known (by me) issues and just works. You might like to try it.

You can always add print statements to the Processing code to see what it’s doing. The Arduino can be more difficult. Please say what Arduino you have, and whether you have anything on it to help see what it’s doing (other serial connections, leds, led screens…).

1 Like