Serial monitor multiple inputs

Hello,

I face a problem if I have to give in multiple parameters in the void setup, I get the first question. if I then put in an nr it skips the next input. If I put a value followed by a space and the second nr it works but I don't get prompt for the second question. Here is an example code.

Thanks and best regards,
Bernard

int redLEDPin=9;
int yellowLEDPin=10;
int redONTime=250;
int redOFFTime=250;
int yellowONTime=250;
int yellowOFFTime=250;
int numYellowBlinks;
int numRedBlinks;
String redMessage="The red LED is Blinking";
String yellowMessage="The yellow LED is Blinking";
String Blink=" You are on Blink #: ";

void setup() {
Serial.begin(9600);
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin,OUTPUT);

Serial.println("How many times you want the red LED to blink? ");
while (Serial.available()==0){ }
numRedBlinks = Serial.parseInt();

Serial.println("How many times you want the yellow LED to blink? ");
while (Serial.available()==0) { }
numYellowBlinks = Serial.parseInt();
}

void loop() {

Serial.println(redMessage);
Serial.println("");

for (int j=1;j<=numRedBlinks;j=j+1) {
Serial.print(Blink);
Serial.println(j);
digitalWrite(redLEDPin, HIGH);
delay(redONTime);
digitalWrite(redLEDPin, LOW);
delay(redOFFTime);
}

Serial.println("");
Serial.println(yellowMessage);
Serial.println("");

for (int k=1;k<=numYellowBlinks;k=k+1){
Serial.print(Blink);
Serial.println(k);
digitalWrite(yellowLEDPin, HIGH);
delay(yellowONTime);
digitalWrite(yellowLEDPin, LOW);
delay(yellowOFFTime);
}

Serial.println("");

}

Welcome to the forum

The first thing to check is what you have Line Ending set to in the Serial monitor. If it is set to anything but No line ending then the second Serial.available() will be true immediately as it sees the line ending added by the Serial monitor

Please follow the advice on posting code given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

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