Hi I tried to build a simple code to get familiar with the serial. I found that it would skip a while function while i was interfacing with the serial monitor. Specifically the last while function in the loop. Before I could enter anything into the serial monitor the code moves on.
String myName; //Declare a string var
int age;
float height;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Please enter your name: ");
while (Serial.available()==0) {
}
myName = Serial.readString();
Serial.println("Please enter your age? ");
while (Serial.available()==0) {
}
age = Serial.parseInt();
Serial.println("How tall are you? ");
while (Serial.available()==0) {
}
height = Serial.parseFloat();
Serial.println(" ");
Serial.print("Hello ");
Serial.print(myName);
Serial.print(", you are ");
Serial.print(age);
Serial.println(" years old.");
Serial.print("and you are ");
Serial.print(height);
Serial.println(" feet tall");
Serial.println(" ");
}
I get this on the monitor,
...........................................
Please enter your name:
Please enter your age?
How tall are you?
Hello Nathan
, you are 21 years old.
and you are 0.00 feet tall
Please enter your name:
........................................
How can i stop it skipping the last while function, thanks
The parseInt() and parseFloat() methods do not empty the serial buffer. So, after you enter age, the Serial Monitor application might send age or age or age, depending on the (unspecified) setting at the lower right corner of the application.
If you are sending or after the age, there WILL be data in the serial buffer, so the while loop will assume that you are going to read it, and will end immediately so that you can.
PaulS:
The parseInt() and parseFloat() methods do not empty the serial buffer. So, after you enter age, the Serial Monitor application might send age or age or age, depending on the (unspecified) setting at the lower right corner of the application.
If you are sending or after the age, there WILL be data in the serial buffer, so the while loop will assume that you are going to read it, and will end immediately so that you can.
That you don't is your problem.
But it fails even with this heavily "flushed" code.
And NEVER fails when BOTH name and age are blank , just CR.
And it also failed / detected input after Name , but I don't remember which "flush" stopped that.
As a side note - there is noticeable pause ( even at 115200 bauds ) in outputting next text after CR is entered.
String myName; //Declare a string var
int age;
float height;
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println("Please enter your name: ");
Serial.flush();
while (Serial.available()==0) {
}
myName = Serial.readString();
Serial.println("Please enter your age? ");
Serial.flush();
while (Serial.available()==0) {
}
age = Serial.parseInt();
Serial.flush();
Serial.println("How tall are you? ");
Serial.flush();
while (!Serial.available()) {
}
height = Serial.parseFloat();
//}while(!height);
Serial.flush();
Serial.println(height);
//for(;;);
Serial.print("Hello ");
Serial.print(myName);
Serial.print(", you are ");
Serial.print(age);
Serial.println(" years old.");
Serial.print("and you are ");
Serial.print(height);
Serial.println(" feet tall");
Serial.println(" ");
Serial.flush();
}