Hi all, I'm brand new to Arduino (and programming) so hopefully this will be something simple I'm missing.
I'm using
while (Serial.available()==0) { }
to wait for input from user via the serial monitor. This is the second time I've run into issues where this line of code will work fine for the first or second time in a program but then the program just blows through it. In this case the program waits for the first two inputs but then skips past the 3rd. So the value for the third variable "my height" is always 0.
This is just some practice code to learn how to use strings, integers and floats. I'll paste the full code below.
Thanks for any ideas!
String myName;
int myAge;
float myHeight;
void setup() {
Serial.begin(57600);
}
void loop() {
Serial.println ("What is your name?"); //Prompt user for name
while (Serial.available()==0) { } //Wait for user input
myName = Serial.readString(); //Read user input and assign to variable myName
delay (250);
Serial.println ("How old are you?"); //Prompt user for age
while (Serial.available()==0) { } //Wait for user input
myAge = Serial.parseInt(); //Read user input and assign to variable myAge
delay (250);
Serial.println ("How tall are you?"); //Prompt user for height
while (Serial.available()==0) { } //Wait for user input
myHeight = Serial.parseFloat(); //Read user input and assign to variable myHeight
delay (250);
Serial.println(" ");
Serial.println(" ");
Serial.print ("So let me see if I got all that. Your name is ");
Serial.print (myName);
Serial.print(", you are ");
Serial.print(myAge);
Serial.print(" years old, and you are ");
Serial.print(myHeight);
Serial.print(" feet tall.");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
delay (5000);
}
Serial is slow; you might only have received one byte and your while (Serial.available()==0) { } will no longer be true so your Serial.readString() will only have one byte to read. Serial.readString() will wait for a while for additional characters but not forever.
First solution is to use Serial.readStringUntil().
Set the Serial Monitor to send a 'newline' and use Serial.readStringUntil('\n').
Better solution is to get rid of String (capital S) and get ideas from the updated Serial Input Basics thread.
Problem with String (capital S) is that extensive use of it (specifically concatenations) can result in unexpected behaviour at run time; better forget that it exists on Arduino.
Serial is slow; you might only have received one byte and your while (Serial.available()==0) { } will no longer be true so your Serial.readString() will only have one byte to read. Serial.readString() will wait for a while for additional characters but not forever.
I don't think that's the problem. The default timeout for Serial.readString(), Serial.parseInt(), and Serial.parseFloat() is 1000 ms. There's no way it should take anywhere near that long to send this data.
LeafOnTheWind:
the program just blows through it. In this case the program waits for the first two inputs but then skips past the 3rd. So the value for the third variable "my height" is always 0.
What do you have selected from the line ending menu near the bottom right of Serial Monitor? The program only works correctly for me with "No line ending" selected. With "No line ending" selected, it works fine for me.