problem using while

this is my code:

void setup() {



void loop() {

Serial.println("Enter number: ");
while (Serial.available()==0);
int val=Serial.parseInt();


when I execute the Sketch and input 5 I expect this:

Enter number:
Enter number:

But what I get is this:

Enter number:
Enter number:
Enter number:

What am I doing wrong?

Have a look at this example of how ‘serialavailable’ works,

You are basically only looking for data when there is none ; If you use : while(Serial.available()==0){ }

Then it will wait in this loop whilst no data is available, and when there is some execute the code that follows .

@hammy, that would not make a difference as far as I know; there is a semi-colon after the while.

@mac-7, what is the line-ending in serial monitor set to? Set it to 'none' if it isn't.


Moved your topic to it’s current location / section as it is more suitable.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.