Question(s) about the Serial.available function

I am in the process of teaching myself to code in Arduino using Paul McWhorter’s videos as a guide, but liberally experimenting on my own. I have run into a problem reading data from the Serial Monitor.

The program excutes the redQuery function correctly. But when it executes the greenQuery function it always returns a zero and never pauses to allow user input.

My suspicion is that there are characters that remain in the serial buffer, so the next call to Serial,available returns a positive number and it blows through.

My questions:

Are my assumptions correct as to what is happening?
Why is the greenQuery returning a zero?
If my assumptions are correct, how do I clear the buffer after the redQuery call?
If my assumptions are wrong, what am I missing?

My code:

/*Reading data from Arduino Serial Monitor
 * This is a practice exercise to learn how
 * to extract data from the the Serial Monitor
 * and implement a user interface
 */

int redLED=9;       //declaring redLED as an integer, and set it to 9
int greenLED=10;    //declaring greenLED as an integer, and set it to 10
int redOnTime=250;  //create variable for red LED time on
int redOffTime=250;  //create variable for red LED time off
int greenOnTime=250;  //create variable for green LED time on
int greenOffTime=250; //create variable for green LED time off
int numRedBlink;    //number of times red will blink
int numGreenBlink;    //number of times green will blink
String redMessage = "The red LED is blinking";  //declaring a string variable
String greenMessage = "The green LED is blinking"; //declaring a string variable

void setup() {
  Serial.begin(9600);  //open the serial port and set it to 9600 baud  
  pinMode(redLED,OUTPUT);   //assign pin 9 as an output
  pinMode(greenLED,OUTPUT); //assign pin 10 as an output  
  Serial.println("Welcome to my Program");
  Serial.println();
}

void loop() {
redQuery();       //call the redQuery function
greenQuery();     //call the greenQuery function
redCountdown();   //call the redCountdown function
greenCountdown(); //call the greenCountdown function  
}

void redQuery()  {
  Serial.println("How many times do you want to blink red LED?"); //print red query
  while (Serial.available()==0){}   //wait for data entry
  numRedBlink = Serial.parseInt();  //assign variable to return value
  Serial.println (numRedBlink);     //print the return value
  Serial.println();                 //skip a line
}

void greenQuery()  {
  Serial.println("How many times do you want to blink green LED?"); //print green query
  while (Serial.available()==0){}     //wait for data entry
  numGreenBlink = Serial.parseInt();  //assign variable for return value
  Serial.println (numGreenBlink);     //print the return value
  Serial.println();                   //skip a line
}

void redCountdown() {
  Serial.println(redMessage);                //print string variable
     for (int j=1; j<=numRedBlink; j++) {    //start our 'for' loop
  Serial.print("  You are on blink #: ");    //print character string
  Serial.println(j);                         //print for loop variable 'j'
  digitalWrite(redLED,HIGH);    //turn on the red LED
  delay (redOnTime);            //wait a bit
  digitalWrite(redLED,LOW);     //turn off the red LED
  delay (redOnTime);            //wait a bit
   }
   Serial.println();            //skip a line
}

void greenCountdown() {
  Serial.println(greenMessage); //print character string
for (int j=1; j<=numGreenBlink; j++){  //start our 'for' loop
  Serial.print("  You are on blink #: ");  //print character string                                          
  Serial.println(j);                      //print for loop variable 'j'
  digitalWrite (greenLED,HIGH); //turn on the green LED
  delay (greenOnTime);          //wait a bit
  digitalWrite (greenLED,LOW);  //turn off the green LED
  delay (greenOnTime);          //wait a bit
  }
Serial.println( );  //skip a line
}

And here is the output on the serial monitor:

Welcome to my Program
How many times do you want to blink red LED?
3
How many times do you want to blink green LED?
0
The red LED is blinking
** You are on blink #: 1**
** You are on blink #: 2**
** You are on blink #: 3**
The green LED is blinking
How many times do you want to blink red LED?
2
How many times do you want to blink green LED?
0
The red LED is blinking
** You are on blink #: 1**
** You are on blink #: 2**
The green LED is blinking
How many times do you want to blink red LED?

What is the line-ending setting on the serial monitor?

Just before:  while (Serial.available()==0){}     //wait for data entry
Add:  while (Serial.available()){Serial.read();}     //Throw away any characters in the buffer

Thanks John & AWOL

Set the Serial Monitor to NEWLINE and inserted recommended code. Program works as intended! :slight_smile: