Serial Monitor user interaction


I have this problem when i used this code in my project. I used as a feedback to Serial.println("Insert a value") 10 and this happens. Is this a bug or i did a mistake?

Yes, and what'?

Change the line ending control to "none"


Or empty the input buffer after you read the number:

  val = Serial.parseInt();

  delay(100); // allow time for more characters to arrive.
  while (Serial.available());  // Throw away character

This will throw away any line-ending characters and anything typed after the number. Advantage: The user doesn't have to know and set the proper line ending. Disadvantage: If you put more than one number on the input line, only the first will be shown.

You could get fancy and allow for both by using Serial.peek() to check each character and only throw away characters until you get to a '-' or a '0' through '9'.

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