When asking for user input it skips over the second input request

So I'm learning Arduino via Youtube and was trying to modify this code a little and experiment.

The program worked fine up until I added the variable user inputs. It will sit there displaying the first two lines of text as expected and I enter an number, then somehow a 0 get put in for the input for the second variable, and then it sits and waits for the third input, skips the fourth just like the second and waits for the fifth input.

Anyone know how to correct this so it doesn't put a 0 in for input 2 and 4?

Oh btw the program is to ask a user how many times a set of 5 LEDs should blink

Thanks in advance for anyone who responds

This is what is displayed
"Welcome to my Program
How many times do you want the Red LED to blink?
2
How many times do you want the Green LED to blink?
0
How many times do you want the Blue LED to blink?
3
How many times do you want the Yellow LED to blink?
0
How many times do you want the White LED to blink?"

/*This program is designed to ask a user for input on how many times each of 5 LED's should blink, start program and continue in that pattern*/
 
int redLED=9;                   // declare pin 9 as the red LED 
int greenLED=10;                // declare pin 10 as yellow LED
int blueLED=11;                 //Blue LED to pin 11
int yellowLED=2;                //yellow LED to pin 2
int whiteLED=3;                 //white LED to pin 3
int redOnTime=170;               // Red LED on time
int redOffTime=50;                //Red LED off time
int greenOnTime=170;            // Green LED on time
int greenOffTime=50;            // Green LED off time
int numRedBlink;                 // Number of times to blink LED
int numGreenBlink;              // Number of times to blink LED
int numBlueBlink;               // Number of times to blink LED
int numYellowBlink;             // Number of times to blink LED
int numWhiteBlink;              // Number of times to blink LED
int blueOnTime=170;             // blue LED on time
int blueOffTime=50;             //blue LED off time
int yellowOnTime=170;             // yellow LED on time
int yellowOffTime=50;                    //yellow LED off time
int whiteOnTime=170;                          //  white LED on time
int whiteOffTime=50;                                //white LED off time
String redMessage="The Red LED is blinking";
String greenMessage="The Green LED is blinking";
String blueMessage="The Blue LED is blinking";
String yellowMessage="The Yellow LED is blinking";
String whiteMessage="The White LED is blinking";

void setup() {
  Serial.begin(9600);
  String wm1= "Welcome to ";     //Declare a string variable wml and assign a value
  String wm2= "my Program";      //Declare string variable wm2 and assign a value
  String wm3;                   // Declare string variable wm3
  wm3= wm1+wm2;                  // concatanating wm1 and wm2 into wm3
  Serial.println(wm3);
  
  pinMode(redLED,OUTPUT);
  pinMode(greenLED,OUTPUT);
  pinMode(blueLED,OUTPUT);
  pinMode(yellowLED,OUTPUT);
  pinMode(whiteLED,OUTPUT);
  
  Serial.println("How many times do you want the Red LED to blink?");             //Ask for input
  while(Serial.available()==0){ }                                                 //Twiddle thumbs until you receive input   
  numRedBlink = Serial.parseInt();                                                  //Read user input
Serial.println(numRedBlink);                                                        //Displays number user inputed

   Serial.println("How many times do you want the Green LED to blink?");           //Ask for input
  while(Serial.available()==0){ }                                                  //Twiddle thumbs until you receive input 
  numGreenBlink = Serial.parseInt();                                                 //Read user input
Serial.println(numGreenBlink);                                                        //Displays number user inputed

 Serial.println("How many times do you want the Blue LED to blink?");                //Ask for input
  while(Serial.available()==0){ }                                                    //Twiddle thumbs until you receive input  
  numBlueBlink = Serial.parseInt();                                                 //Read user input
Serial.println(numBlueBlink);                                                        //Displays number user inputed

 Serial.println("How many times do you want the Yellow LED to blink?");              //Ask for input
  while(Serial.available()==0){ }                                                    //Twiddle thumbs until you receive input    
  numYellowBlink = Serial.parseInt();                                                 //Read user input
Serial.println(numYellowBlink);                                                        //Displays number user inputed

 Serial.println("How many times do you want the White LED to blink?");               //Ask for input
  while(Serial.available()==0){ }                                                     //Twiddle thumbs until you receive input 
  numWhiteBlink = Serial.parseInt();                                                 //Read user input
Serial.println(numBlueBlink);                                                        //Displays number user inputed
}

void loop() {
  
 
  Serial.println(redMessage);
 for (int j=1; j<=numRedBlink; j=j+1) {
  Serial.print("You are on blink #:");
  Serial.println(j);
 digitalWrite(redLED,HIGH);     // Turn the red LED on
 delay(redOnTime);
 digitalWrite(redLED,LOW);      // Turn off the red LED
 delay(redOffTime);

}
 Serial.println(" ");
 
 Serial.println(greenMessage);
for (int k=1; k<=numGreenBlink; k=k+1) {
  Serial.print("You are on blink #:");
 Serial.println(k);
 digitalWrite(greenLED,HIGH);     // Turn the green LED on
 delay(greenOnTime);
 digitalWrite(greenLED,LOW);      // Turn off the green LED
 delay(greenOffTime);

}

  Serial.println(" ");
  
  Serial.println(blueMessage);
 for (int m=1; m<=numBlueBlink; m=m+1) {
  Serial.print("You are on blink #:");
  Serial.println(m);
 digitalWrite(blueLED,HIGH);     // Turn the blue LED on
 delay(blueOnTime);
 digitalWrite(blueLED,LOW);      // Turn off the blue LED
 delay(blueOffTime);

}
  Serial.println(" ");
 
 Serial.println(yellowMessage);
 for (int n=1; n<=numYellowBlink; n=n+1) {
  Serial.print("You are on blink #:");
  Serial.println(n);
 digitalWrite(yellowLED,HIGH);     // Turn the yellow LED on
 delay(yellowOnTime);
 digitalWrite(yellowLED,LOW);      // Turn off the yellow LED
 delay(yellowOffTime);

}

  Serial.println(" ");
  Serial.println(whiteMessage);
 for (int o=1; o<=numWhiteBlink; o=o+1) {
  Serial.print("You are on blink #:");
  Serial.println(o);
 digitalWrite(whiteLED,HIGH);     // Turn the white LED on
 delay(whiteOnTime);
 digitalWrite(whiteLED,LOW);      // Turn off the white LED
 delay(whiteOffTime);

  Serial.println(" ");
 }
 }

sketch_may05b.ino (5.93 KB)

When you write the number and press return, the terminal sends the chars of your number followed by a newline (and maybe a carriage return)

Then Serial.parseInt will read the number and will stop at newline, that will remain in the serial buffer.

After printing your number and the message asking for next number, the program checks Serial.available, that will be > 0 because at least newline is in the buffer, so Serial.parseInt is called immediately, skips newline (and carriage return if present) and after the time out (1 second default) returns with 0

To solve this problem, empty the serial buffer after every parseInt call

For example, the modified code for red blink can be

Serial.println("How many times do you want the Red LED to blink?");

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

// discard newline and carriage return after the number
while(Serial.available() > 0)
	Serial.read();
	
Serial.println(numRedBlink);

@OP

Why are you getting 0?

1. Let us first look into the structure of the Serial Monitor via which you send your number to UNO.
SerialMonitor.png
Figure-1: Serial Monitor of Arduino UNO

(1) Look into the 'Line ending tab'. It has three options:
Newline -- if it is selected, the code 0x0A (00001010) will be sent to UNO after sending the number(s) that you have placed in the InputBox of Serial Monitor (Fig-1).

Carriage return -- if it is selected, the code 0x0D (00001101) will be sent to UNO after sending the number(s) that you have placed in the InputBox of Serial Monitor.

Both NL & CR -- if it is selected, the code 0x0D and then code 0x0A will be sent to UNO after sending the number(s) that you have placed in the InputBox of Serial Monitor.

2. Your problem (appearance of 0 when you have not entered anything) will be solved if you select 'No line ending' option. Try and see the result.

3. Select the option Newline; now 0 appears. Let us try to understand how it does come.
(1) Two data items have gone from the Serial Monitor to the UNO and they have entered into the Serial Buffer of UNO. These data items are: 0x02 and 0x0A (0x indicates that the numbers what they follow it are hexadecimal numbers).

(2) The Serial.parseInt(); function takes out (looks ahead -- sees in advance before making the actual reading) a data byte (in this case it is 0x32 for the digit 2) from the Buffer and checks if it is the ASCII code of a digit (0x30 to 0x39 for 0 to 9). The function reads 0x32 from the buffer as it is a valid code for a valid digit (2). The function applies similar strategy for the next data byte which is 0x0A; the function sees that the code (00001010) is not an ASCII for for any digit of 0 to 9; so, the code is not read out and it remains in the Serial Buffer.

(3) As the Serial Buffer is not empty, the program calls upon the Serial.parseInt() function without waiting for any data item coming from the Serial Monitor. The function sees that data byte present in the buffer is a non-digit and in the meantime the default 1-sec time-out has occurred; the function terminates and returns 0 which we see on the Serial Monitor.

If no valid digits were read when the time-out (see Serial.setTimeout()) occurs, 0 is returned;

SerialMonitor.png