Program ignores while-loop

Hello everyone!
I don't know why but my Arduino Testprogram does not work like it should:

void setup() {
  Serial.begin(9600);
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);

  Serial.println("Number of LED1 blinks? ");
  while (Serial.available()==0){}
  b1 = Serial.parseInt();

  Serial.println("Number of LED2 blinks? ");
  while (Serial.available()==0){}
  b2 = Serial.parseInt();

It is just a test where I am asking in the serial monitor for an input.
What now happens is, that there is the first question: "Number of LED1 blinks?"
after the Input, the second question appears and shortly after the program starts (the void loop) without waiting for the input :frowning:

The second while loop does not work somehow...

This works for me on a Leonardo :

void setup() {
  Serial.begin(115200);
  while (!Serial);  //  <-- For Leonardo

  Serial.println("Number of LED1 blinks? ");
  while (Serial.available() == 0) {}
  int b1 = Serial.parseInt();
  Serial.println(b1);

  Serial.println("Number of LED2 blinks? ");
  while (Serial.available() == 0) {}
  int b2 = Serial.parseInt();
  Serial.println(b2);
}

void loop() {
}

Serial monitor displays:

10:07:56.350 -> Number of LED1 blinks?
10:07:58.429 -> 11
10:07:58.429 -> Number of LED2 blinks?
10:07:59.410 -> 20

What is your board?

I'm going to guess you have the monitor's line ending set to something other than no line end, so the presence of line end character/s is making it fail the second test.

line end and baud.GIF

My setting here is indeed "newline", but the other settings give the same result

Well I did a quick test and it works fine with "no line ending", and gives your OP's symptom with any other.

lesept:
My setting here is indeed "newline", but the other settings give the same result

Sorry, I thought that reply was from OP.

OP's code works for me on Uno with no line ending, but gives OP's symptom with the others.

Something to do with Leonardo's USB?

Your line ending in the Serial Monitor is set to "Both NL & CR" (or you send more then just numbers when you send you input (including spaces). parseInt() breaks on the NL (new line) but that leaves the CR in the buffer.

Damnit, I forgot to hit post...

I ran this code:

byte LED1=2;
byte LED2=3;

void setup() 
{
  Serial.begin(9600);
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  Serial.println("start");

  Serial.println("Number of LED1 blinks? ");
  while (Serial.available()==0){}
  int b1 = Serial.parseInt();
  Serial.println(b1);
  

  Serial.println("Number of LED2 blinks? ");
  while (Serial.available()==0){}
  int b2 = Serial.parseInt();
  Serial.println(b2);
  Serial.println("end");

}
void loop(){}

And here are my serial spews:

start
Number of LED1 blinks? 
5
Number of LED2 blinks? 
3
end
start
Number of LED1 blinks? 
5
Number of LED2 blinks? 
0
end
start
Number of LED1 blinks? 
5
Number of LED2 blinks? 
0
end
start
Number of LED1 blinks? 
5
Number of LED2 blinks? 
0
end

Only the first one waits for input (the 3), the others fly right through and print the b2's initialised value (the 0).

Thank you guys! Now it is working.
With Newline it does not work - with no line ending it does :slight_smile:

But what happens here? I work with an Arduino Mega2560.

Erazor85:
But what happens here?

The line end character/s which are the flags that your integer has ended, are also characters in their own right so they count towards the second "while (Serial.available()==0)" test not being 0, even though you only typed one key.

In other news, it may be important to you one day that Serial.parseInt() is a blocking function. You may have noticed a lag in your example until the timeout kicked in; I did.

If it matters one day, eg where the responsiveness of buttons is crucial, it would behoove you to read Robin2's serial tutorial.

Okay, thank you for the information - sounds logic :slight_smile: