while(Serial.available()==0) {} loop falls through

Good!!!

A suggestion don't use Serial.parseInt() after each while - that kinda works because you type a zero but that function will wait until it completed reading a full int - which happens if it can read something that looks like an int (you zero) and then another char that is not a number arrives. As you just type 1 char and nothing that looks like the end of an int, the function call will wait until timeout which is set by default to 1 second => so that slows down your user interface and your program will wait 1 sec after you press enter before continuing.

What I suggest when you just want to wait and don't care about what is being typed in, is doing another loop with while something is in serial.available then read it with a serial.read. Once this is done then your program will continue - so instead of waiting for a 1s timeout you will just have a few microseconds after you typed zero+enter for your program to continue its execution

  while(Serial.available()==0) {}; // wait for 1 keypress
  while(Serial.available() > 0) {Serial.read()}; // we had at least 1 char, empty the buffer from all input
 // here we are good to go with an empty buffer and we did not wait for any timeout

Hope this helps

1 Like