Help With While Statement

I am trying to figure out why this code is not working as expected. It's pretty basic, I know. I am just starting out and trying to learn my way around.

When this is run (in the setup statement), the code stops at the first While statement and waits for user input as expected. However, when I enter a number and hit Enter or Send, it asks the second question, but does not wait for input at the second While statement. It jumps right into the loop code.

For the life of me I don't see what I may have done wrong or why it is not doing what I think it should. Anyone have any input?

  Serial.println("Enter the number of times the Red LED should blink: ");
  
  while ( Serial.available() == 0 ) { }
  
  rBlinkCount = Serial.parseInt();



  Serial.println("Enter the number of times the Yellow LED should blink: ");
  
  while ( Serial.available() == 0 ) { }
  
  yBlinkCount = Serial.parseInt();

Look at the bottom of the serial terminal, to the left of the baud rate, and see what you have the line ending set to. If you have it set to anything except "No line ending", there will be an additional character sent after the number you enter (either a newline or carriage return, or both). Serial.parseInt() leaves this character in the serial buffer, so the code immediately goes to the second Serial.parseInt() statement, which times out after a second because it finds no more numbers in the input.

david_2018:
Look at the bottom of the serial terminal, to the left of the baud rate, and see what you have the line ending set to. If you have it set to anything except "No line ending", there will be an additional character sent after the number you enter (either a newline or carriage return, or both). Serial.parseInt() leaves this character in the serial buffer, so the code immediately goes to the second Serial.parseInt() statement, which times out after a second because it finds no more numbers in the input.

THANK YOU! I would have never figured that out.

Is there a way to prevent this programmatically? Or a better way to do this particular task?

Yes, keep reading (and ignoring) single characters while there is at least one character available. I could provide the code and tell you where to put it but I sense that you may prefer to do this yourself.

vaj4088:
Yes, keep reading (and ignoring) single characters while there is at least one character available. I could provide the code and tell you where to put it but I sense that you may prefer to do this yourself.

I would prefer to do so myself, and thank you for that. That gives me something to go on and I will see what I can come up with.

RedDirtCyclist:
I would prefer to do so myself, and thank you for that. That gives me something to go on and I will see what I can come up with.

The task will be easier to do if we know what the following code is doing by breaking it down into multi-line codes:

while ( Serial.available() == 0 ) 
{ 
    
}

When a characters is sent to the UNO from the InputBox of the Serial Monitor (Fig-1), the character is captured by UNO and is saved in a FIFO (first-in first-out) type buffer. So, a user program first checks if the buffer has accumulated any (at least one) character and then the user brings out the character from the buffer into a variable. That means that the user program keeps checking the content of the buffer until a non-zero value is found. Therefore, the following codes are in logical order:

void loop()
{
    byte n = Serial.available();          //read the content of buffer
    if(n !=0 )                                    //buffer contains at least one character
    {
         char x = Serial.read();           //bring out the character from buffer and save in x
    }
}                                                   //buffer holds no character/data byte; check again

If you are asked to reduce the 3-line codes of the of the above sketch, you would certainly come up with the following answer:

void loop()
{
    while(Serial.available() == 0)       //keep reading the buffer until it holds a character/data byte
    {
         
    }
    char x = Serial.read();
}

Serial Monitor
SerialMonitor.png
Figure-1: Detailed structure of Serial Monitor

(1) If 'Newline' option is selected in the line ending tab, the Serial Monitor sends 0x0A (ASCII code for Newline (\n)) at the end of sending the character (s) of the InputBox.

(2) If 'Carriage return' option is selected in the line ending tab, the Serial Monitor sends 0x0D (ASCII code for Carriage return (\r)) at the end of sending the character (s) of the InputBox.

(3) If 'Both NL & CR' option is selected in the line ending tab, the Serial Monitor sends 0x0D and then 0x0A (ASCII codes for Carriage return and Newline (\r\n)) at the end of sending the character (s) of the InputBox.

(4) If 'No line ending' option is selected in the line ending tab, the Serial Monitor sends no code at the end of sending the character (s) of the InputBox.

SerialMonitor.png