Arduino Will Not Pause on Second Empty While Loop

Hi All!
New to Arduino and new to the forum!

I am going through the basic tutorials and while writing a program with serial monitor input, I read / watched that to wait for using input you can use Serial.available() in an empty while loop to pause. I am trying to do this twice to get 2 pieces of input from the user. The program compiles and sends to Arduino. I get the first pause until input is entered, it goes to the second prompt for a moment and then immediately skips out of the second empty while loop to the remaining loops.

Code is:

int redLedPin = 8; // Set the Pin for the Red LED
int blueLedPin = 13; // Set the Pin for the Blue LED
int redLedOnTime = 200; // Set the On Time for the Red LED
int blueLedOnTime = 200; // Set the On TIme for the Blue LED
int redLedOffTime = 200; // Set the Delay Time for the Red LED
int blueLedOffTime = 200; // Set the Delay Time for the Blue LED
int redBlinkNum; // Set the Number of tImes to blink the red LED
int blueBlinkNum; // Set the Number of Times to blink the Blue LED
String redLedMsg = " The Red LED is Blinking ";
String blueLedMsg = " The Blue LED is Blinking ";


void setup() {
 Serial.begin(9600); // Begin Serial Monitor at 9600 bits per second ( Baud Rate )
 pinMode(redLedPin, OUTPUT); // Set the Pin mode for the Red LED as an OUTPUT
 pinMode(blueLedPin, OUTPUT); // Set the Pin Mode for the Blue LED as an OUTPUT
 }

void loop() {
 Serial.println("Enter The Number Of Blinks for the Red LED: "); // Prompt User for Input
  while (Serial.available()==0){ } // Wait for the User to Input Data
  redBlinkNum = Serial.parseInt(); // Set the value of redBlinkNum to input data from user
 
 Serial.println("Enter the Number of Blinks for the Blue LED: "); // Promt User for input again
  while (Serial.available()==0){ } // Wait for Second Input from user
  blueBlinkNum = Serial.parseInt(); // Set the value of blueBlinkNum to second input
  
 Serial.println(redLedMsg);
  for(int j=1; j <= redBlinkNum; j++) // Sets the Number of Times to blink RED LED
  { Serial.print("You Are On Blink # ");
    Serial.println(j);
    digitalWrite(redLedPin, HIGH); // Turn on the Red LED
    delay(redLedOnTime); // Stay on for the amount of time specified above
    digitalWrite(redLedPin, LOW); // Turn off the RED LED
    delay(redLedOffTime); // Turn on for amount of time specified above
  }
Serial.println(blueLedMsg);
  for(int j=1; j <= blueBlinkNum; j++) // Sets the Number of Time to blink Blue LED
  { Serial.print("You Are On Blink # ");
    Serial.println(j);
    digitalWrite(blueLedPin, HIGH);
    delay(blueLedOnTime);
    digitalWrite(blueLedPin, LOW);
    delay(blueLedOffTime);
  }
}

If I hit a number ( int ) quickly after entering the first, the program runs as expected, re-prompts for the first input and quickly then skips to the second input prompt and waits.

Any Thoughts or assistance is appreciated.

Obviously it's not a crucial application, but I want to learn where I am going wrong or if there is a better way to gain multiple pieces of input from a user prior to running other loops.

Thanks!

Your problem is probably that parseInt uses a timeout; so if you don't type fast enough, your code will go to the next 'wait'.

The code for parseInt can be found in (on a windows system) C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\Stream.cpp (just in case you're interested).

Read the Serial Input Basics - updated thread to get ideas how to read and parse serial input.

I have a post about handling serial input without blocking - that might help you.

  while (Serial.available()==0){ }
  redBlinkNum = Serial.parseInt();

Hi stackhouse. That simple code does work, but you need to set your serial monitor to not send any line ending characters. (See drop down selection at the bottom of the serial monitor window).

The reason why this code works (and doesn't just stop after the first character is read) is that parseInt has a delay built in (otherwise parseInt could consume characters far faster than the serial port could receive new ones). It actually stops for a while waiting for new characters to arrive before timing out and considering the number finished.

This is convenient, because it allows simple code like the above to work. However it can also make a noticeable delay between entering your number and the program responding (you may notice this after you turn off all line end characters), and this can be a nuisance in some situations.

One other alternative (to turning off any line end characters) is to just empty the input buffer after parseInt. For example:

  while (Serial.available()==0) {}
  redBlinkNum = Serial.parseInt();
  while (Serial.available() != 0) {char dummy = Serial.read();}

EDIT: I just had a thought that perhaps the above might not work with multiple line end characters (both CR and LF), not without yet another delay anyway. TBH I've never tested that. Anyway, it definitely works perfectly for single line end characters (either CR or LF), which is usually all you need.

Yes!

"but you need to set your serial monitor to not send any line ending characters. (See drop down selection at the bottom of the serial monitor window"

This Worked! I changed to "No Line Ending" and the program works as expected. I was following along with a tutorial and saw that it worked on their screen, but they did not comment on Line Ending, NL, or Carriage Return settings.

Looks like I have further reading this evening.

Thank you everyone for the response. I did read the posting: handling serial input without blocking - and it seems there is a way to code for this, too, without having to change the serial monitor settings.

Thank you everyone for the fast responses and assistance.