Problem with changing input variable and serial monitor

Hello, I would like to learn Arduino. I'm trying to modify a "simple" program (LEDS). For this I use the serial monitor for entering values. In the added sketch you can see the program.

I would like to achieve the following: a) Entering a value for the yellow and a value for the red LED. b) After this I would also like to enter separate values for the "time on" and the "time off" of the red and yellow LED.

  1. I want to enter a value for the yellow and red LEDs via the serial monitor. The input of 1 LED goes well and also carries out the command of this LED. If you immediately want to enter the value of the second LED, this will be skipped. In the program you can see that you can enter both values one after the other. If I "disable" the first part, it works fine for the second part. If I disable the 2nd part, the 1st part works fine. In this program it is not possible to enter 2 values and have them both flash.

I've tried everything but haven't found a solution yet.
Who can help me learn Arduino?
Thank you in advance

The program:

// Hier is de opdracht dat je uiteindelijk alle vaste waardens variabel gaat 
// maken zodat je deze via invoer seriele monitor deze waardens kunnen wijzigen.
// We beginnen met Yellow
int redLEDPin=9; //Declare redLedPin an int, and set to pin 9
int yellowLEDPin=10; //Declare yellowLEDPin an int,and set to pin 10
int redOnTime=500; //Declare redOnTime an int, and set to ?? msec
int redOffTime=500; //Declare redOffTime an int,and set to ?? msec
int yellowOnTime=1000; // Declare yellowOnTime an int, and set ?? msec
int yellowOffTime=1000; // Declare yellowOffTime an int, and set to ?? msec
int numYellowBlinks; // Number of times to blink yellow Led 
int numRedBlinks; // Number of times to blink red Led 

String redMessage= "The Red LED is Blinking"; // Declaring a String Variable
String yellowMessage= "The Yellow LED is Blinking"; // Declaring a String Variable
String OnYellow= "On Time yellow ="; // Time yellow is on
String OnRed= "On Time red ="; // Time red is on
String OffYellow= "Off Time yellow ="; // Time yellow is off
String OffRed= "Off Time red ="; // Time red is off


 
void setup() {  
  Serial.begin(9600);        // Turn on the Serial Port
  pinMode(redLEDPin, OUTPUT);  // Tell Arduino that redLEDPin is an output pin
  pinMode(yellowLEDPin, OUTPUT);  //Tell Arduino that yellowLEDPin is an output pin
} 

void loop() {
 
Serial.println("How Many Times Do You Want The Red LED to Blink?");
while (Serial.available() ==0){ } // Waiting for input
numRedBlinks = Serial.parseInt();// Read user input

Serial.println("How Many Times Do You Want The Yellow LED to Blink?");
while (Serial.available() ==0){ } // Waiting for input
numYellowBlinks = Serial.parseInt();//Read user input

Serial.print("Het aantal knipperen is nu: ");
Serial.println(numYellowBlinks );

//Serial.println(redMessage);
  for (int j=1; j<=numRedBlinks; j=j+1) {     // Start our for loop
    Serial.print("   You are on Blink #: ");
    Serial.println(j);
    digitalWrite(redLEDPin,HIGH); //Turn red LED on
    delay(redOnTime);             //Leave on for redOnTime
    digitalWrite(redLEDPin,LOW);  //Turn red LED off
    delay(redOffTime);            //Leave off for redOffTime
}
Serial.println(" ");
Serial.println(yellowMessage);
  for (int j=1; j<=numYellowBlinks; j=j+1) {     // Start our for loop
    Serial.print("   You are on Blink #: ");
    Serial.println(j);
    digitalWrite(yellowLEDPin,HIGH); //Turn yellow LED on
    delay(yellowOnTime);             //Leave on for yellowOnTime
    digitalWrite(yellowLEDPin,LOW);  //Turn yellow LED off
    delay(yellowOffTime);            //Leave off for yellowOffTime
}
Serial.println(" ");
}

The serial monitor shows this:

How Many Times Do You Want The Yellow LED to Blink?
Het aantal knipperen is nu: 0
You are on Blink #: 1
You are on Blink #: 2
You are on Blink #: 3
You are on Blink #: 4
You are on Blink #: 5

The Yellow LED is Blinking

How Many Times Do You Want The Red LED to Blink?
How Many Times Do You Want The Yellow LED to Blink?
Het aantal knipperen is nu: 0
You are on Blink #: 1
You are on Blink #: 2
You are on Blink #: 3

The Yellow LED is Blinking

How Many Times Do You Want The Red LED to Blink?

What is the line-ending set to in the serial monitor. The default is <CR> plus <NL>.

After the first parsInt, you still have those in the buffer and hence it runs through the while (Serial.available() ==0){ }.

If you change the line-ending to 'none', your problem is probably solved.

PS
You can read Serial Input Basics - updated to get some ideas how to properly handle serial input.

Thanks Sterretje.
Followed the advice. It works now.
Finally selected "change the end of the line to 'none'" in the Serial Monitor parameters.

I find it strange that you have to do this via the Serial Monitor. Isn't this also possible with software?

Now continue working on the assignment: Enabling how to adjust all integer variables (Time, numbers, etc).

Thank you

One approach is in the link that I provided.

note that your program is using the String class
if you are using a low power microcontroller like the UNO or Mega it is recommended that you avoid using the string class as its use can fragment memory causing unpredictable errors and system crashes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.