while loop problems

got a problem with this while loop.

the first while loop prompts user for input and waits for that input, and put the input it in a variable, this one works fine, but then the next while loop should do the same, but it does not wait for input it just continues regardless if user enters something or not.

if you are quick enough and enters a second value, it will put that value into the variable.

Do i need to clear something first ??

sketch_may24a.ino (2.7 KB)

Your code:-

Serial.println("indtast antal gange du ønsker den røde lysdiode skal blinke: ");  //prompt bruger for input
  while (Serial.available()==0){}                                                   //vent på input
  redNumBlink=Serial.parseInt();                                                    //put brugerindput i variabel
  
  Serial.println("indtast antal gange du ønsker den grønne lysdiode skal blinke: ");//prompt bruger for input
  while (Serial.available()==0){}                                                   //vent på input
  greenNumBlink=Serial.parseInt();                                                  // pit brugerinput i variabel

It looks like the serial buffer has something left in it after the parseInt() call. Try emptying it first like this:-

Serial.println("indtast antal gange du ønsker den røde lysdiode skal blinke: ");  //prompt bruger for input
  while (Serial.available()==0){}                                                   //vent på input
  redNumBlink=Serial.parseInt();                                                    //put brugerindput i variabel
  
  while(Serial.available()!= 0){  // empty the buffer
    Serial.read();
   }

  Serial.println("indtast antal gange du ønsker den grønne lysdiode skal blinke: ");//prompt bruger for input
  while (Serial.available()==0){}                                                   //vent på input
  greenNumBlink=Serial.parseInt();                                                  // pit brugerinput i variabel

thansk mate it worked.

but should it not clear by it self?

Lille_Viking:
thansk mate it worked.

but should it not clear by it self?

Serial.parseInt() stops when a non digit character is read, such as linefeed or carriage return. What have you got the Line ending set to in the Serial monitor ?

linechange it would apear

juste changed it to no lineend, and remove the code, and that works to. but it was nice to know how to clear the buffer