used while loop as shown in tutorial but did not work as shown in tutorial

Hi I have been following Paul McWhorter on youtube and I am having some problems using a while loop so I can tell the code through the serial port the video was posted in 2014.

So I am unsure if there are some changes in the creation of the code with the newer version of the software.

the actual problem is that the serial port will ask for the first setting then will skip the second and allow the 3rd to be set you can add the settings if you are quick but the serial port is not pausing waiting for all settings to be set.

please find my code below:-

int redLEDPin=9; //Declaring redLEDPin as an int, and set to pin 9
int whiteLEDPin=10; //Declaring whiteLEDPin as an int, and set to pin 10
int redOnTime=250; //this is the red LED on time
int redOffTime=250; //this is the red LED off time
int whiteOnTime=250; //this is the white LED on time
int whiteOffTime=250; //this is the white LED off time
int numRedBlink; //number of times the red LED blinks
int numWhiteBlink; //number of times the white LED blinks
String redMessage=“the Red LED is blinking”; //Declaring a String variable
String WhiteMessage=“the White LED is blinking”; //Declaing a String variable

void setup() {

Serial.begin(115200); //serial port on code
pinMode(redLEDPin, OUTPUT); //tell arduino that redLEDPin is an output pin
pinMode(whiteLEDPin, OUTPUT); //tell arduino that whiteLEDPin is an output pin

Serial.println("how many times do you want to blink the RED LED "); //promt user for input for the amoutn of blinks

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

} //wait for input
numRedBlink = Serial.parseInt( ); //Read user input

Serial.println("how many times do you want to blink the WHITE LED "); //promt user input
while (Serial.available()==0){

} //wait for input
numWhiteBlink = Serial.parseInt( ); //read user input
}

void loop() {

Serial.println(redMessage); //start point of the print line for red LED
for (int j=0; j<=numRedBlink; j=j+1 ) { //start for the loop
Serial.print(" you are on blink #: "); //indicating the blink
Serial.println(j); //what part do you want to see in the serial port
digitalWrite(redLEDPin, HIGH); //Turn the red LED on
delay(redOnTime); //red LED on delay timer
digitalWrite(redLEDPin, LOW); //turn the red LED off
delay(redOffTime); //red LED off delay timer

}
Serial.println(" “);
Serial.println(WhiteMessage); //start point of the print line for white LED
for (int j=0; j<=numWhiteBlink; j=j+1 ) { //start for the loop
Serial.print(” you are on blink #: "); //indictaing the blink
Serial.println(j); //what part do you want to see in the serial
digitalWrite(whiteLEDPin, HIGH); //turn the white LED on
delay(whiteOnTime); //white LED on delay timer
digitalWrite(whiteLEDPin, LOW); //turn the white LED off
delay(whiteOffTime); //white LED off delay timer

}
Serial.println(" ");

}

How do people work with the code laid out in such a mess ?

As a public service, here it is Auto Formatted in the IDE and in code tags
OP please take note

int redLEDPin=9;                                  //Declaring redLEDPin as an int, and set to pin 9
int whiteLEDPin=10;                               //Declaring whiteLEDPin as an int, and set to pin 10
int redOnTime=250;                                    //this is the red LED on time
int redOffTime=250;                               //this is the red LED off time
int whiteOnTime=250;                              //this is the white LED on time
int whiteOffTime=250;                             //this is the white LED off time
int numRedBlink;                                  //number of times the red LED blinks
int numWhiteBlink;                                //number of times the white LED blinks
String redMessage="the Red LED is blinking";      //Declaring a String variable
String WhiteMessage="the White LED is blinking";  //Declaing a String variable

void setup() {

  Serial.begin(115200);                 //serial port on code
  pinMode(redLEDPin, OUTPUT);         //tell arduino that redLEDPin is an output pin
  pinMode(whiteLEDPin, OUTPUT);       //tell arduino that whiteLEDPin is an output pin

  Serial.println("how many times do you want to blink the RED LED "); //promt user for input for the amoutn of blinks

  while (Serial.available()==0){ 
    
    }                                              //wait for input
  numRedBlink = Serial.parseInt( );                                              //Read user input


  Serial.println("how many times do you want to blink the WHITE LED ");         //promt user input
  while (Serial.available()==0){ 
    
    }                                               //wait for input
  numWhiteBlink = Serial.parseInt( );                                            //read user input
}

void loop() {
  
Serial.println(redMessage);                     //start point of the print line for red LED
for (int j=0; j<=numRedBlink; j=j+1 ) {         //start for the loop
  Serial.print(" you are on blink #: ");        //indicating the blink
  Serial.println(j);                            //what part do you want to see in the serial port
  digitalWrite(redLEDPin, HIGH);                //Turn the red LED on
  delay(redOnTime);                             //red LED on delay timer
  digitalWrite(redLEDPin, LOW);                 //turn the red LED off
  delay(redOffTime);                            //red LED off delay timer
 
}
Serial.println(" ");
Serial.println(WhiteMessage);                    //start point of the print line for white LED
for (int j=0; j<=numWhiteBlink; j=j+1 ) {        //start for the loop
  Serial.print(" you are on blink #: ");         //indictaing the blink
  Serial.println(j);                             //what part do you want to see in the serial
  digitalWrite(whiteLEDPin, HIGH);               //turn the white LED on
  delay(whiteOnTime);                            //white LED on delay timer
  digitalWrite(whiteLEDPin, LOW);                //turn the white LED off
  delay(whiteOffTime);                           //white LED off delay timer

}
Serial.println(" ");

}

the serial port will ask for the first setting then will skip the second and allow the 3rd to be set

There appear to be only two prompts in the code, not three

UKHeliBob:
How do people work with the code laid out in such a mess ?

The comments don't align!

DKWatson:
The comments don't align!

Guilty, but there is no option available for that in the Auto Formatter setup !

Hi UKhelibob

I have tried the code you have sent through and I am sorry I thought I had copied the code with 3 prompts.

unfortunately you code you have displayed does the same and this is that when the first prompt is shown the code pause's till you set the amount of blinks for the red LED

When the second promt should pause for the same reason it does not pauseit seems to think the LED blink is above 0 and the only way to set the white LED.

you have to press the setting really quick the code should pause on this type of while loop.

I am new to arduino and this type of coding when I copied the code it was displayed very poorly on the site but I try to keep my code very neat.

is there anything anyone can help or see where I have gone wrong so I can correct and understand my mistake.

I suspect that you have the serial monitor set to both NL and CR. Try setting it to Newline.

The reason is that parseint will read characters until it sees something that isn't an int. Newline stops it, but if there's a Carriage return following, the second parseint will see no digits followed by that CR so it will immediately return 0.

unfortunately you code you have displayed does the same

No surprise there. It is your original code reformatted to make it easier to read and in code tags to make it easier to copy to an editor for examination.

What have you got the Line ending set to in the Serial monitor ? Try setting it to No line ending

Hi Wild Bill

when I checked the serial port it was set to new line I did try setting to NL and CR and realised that was not right either when I set back to NEWLINE and unfortunatley the code still did the same.

Hi UKHelibob

thank you I have set the serial port to no line ending and this has created the pause.

thank you to everyone that has helped me find where my mistake was and helping me understand the code and serial port communication.