Conflict between Serial.parseFloat() and Switch Case

Hoping someone can help me.

I have two Arduino Megas connected via serial 1.

Mega Board 1 collects sensor data and outputs it on serial1. I have no problems here.

Mega Board 2 reads the serial data from Mega Board 1 and displays the data. This is where I am having trouble.

(Note: There is a reason I'm using two boards but I've stripped my code down to the bare minimum here for troubleshooting and am using the serial monitor on the second board simply to see if the code is working.)

Here is the issue:

Anytime I use the line "SensorVel = Serial1.parseFloat();" the button effectively fails to switch the cases. I say effectively because if I push the button many times, occasionally the display board with switch cases for me.

If I comment out the Serial1.parseFloat(), the program runs great switching between cases.

The other interesting (aka "frustrating") thing I notice is that when the serial data is displayed, the program seems to slow down quite a bit. I don't know if this is unavoidable due to the time required to read the serial data or if there is anything else I can do. The serial data is just a stream of integers.

Can anyone advise me why the program effectively stops responding to the button being switched anytime I use the Serial1.parseFloat() command and how to solve the problem.

I'm very new to programming the Arduino and to this forum, so please excuse me for any oversights.

Here is the test code:

#define button 22
#define relay 34
int SensorVel = 0;
int DisplaySpeed = 0;
int state = 0;
int old = 0;
int buttonPoll = 0;
int DisplayPwr = 0;


void setup() {
  Serial.begin(19200);
  Serial1.begin(19200);
  pinMode (button, INPUT);
  pinMode (relay, OUTPUT);
  digitalWrite(relay,HIGH);
}


void loop() {

  // MONITOR BUTTON PUSHED TO SELECT MODE
  buttonPoll = digitalRead(button);
  if(buttonPoll == 1){
    delay(50);
    buttonPoll = digitalRead(button);
  if(buttonPoll == 0){
     state = old + 1;
  }}

  else{
  delay(100);
  }


  // SPECIFY WHAT MODE IS USED
  switch (state) {
    
    case 1:
           DisplaySpeed = 0;
           SensorVel = Serial1.parseFloat();
           Serial.print("Case 1: ");
           Serial.println(SensorVel);           
           old = state;
           break;

    
    case 2:
           DisplaySpeed = 0;
//           SensorVel = Serial1.parseFloat();
           Serial.print("Case 2: ");
           Serial.println(SensorVel);           
           old = state;
           break;

    default:    
           digitalWrite(relay,LOW);
           Serial.println("Case 3: ");
           old = 0;
           break;
  }
}

Use:

if(serial1.available() > 0)
{

}

first, to see if there's anything to parse.

Thanks for the fast reply.

Your suggestion helps, but it still only recognizes the button when the sensor reports 0. If any value other than 0 is read, the program seems too consumed with printing the sensor value to be able to read the change in state.

More thoughts?

Thanks very much!

I should add that the sensor only streams data over serial one with motion is detected.

Here is an example of the data being sent over serial1 to the seconds Mega board:

4
2
2
4
1
2
3
2
0
1
1
2
1
1
0
1
2

parseInt() is a blocking function. You can't check for button presses if you're stuck inside it waiting it to finish receiving a complete number. You have to use non-blocking techniques. You should continuously check for a character to be available. When one is, put it in a buffer array and then continue with the loop() function. Only when a terminating character (non-numeral) is detected do you deal with the whole array at one time.

Check out @Robin2's Serial Tutorial. It demonstrates on-blocking techniques.