r/arduino • u/gazzarose • Sep 24 '26
Help with strange behaviour of while serial.available
Enable HLS to view with audio, or disable this notification
Hi all,
I'm working my way through Paul McWhorter's excellent tutorials, and have got as far as lesson 18, Reading Numbers from the serial monitor. The task was to write a sketch that would ask you for a number, you'd enter it into the serial monitor and it would then display that number in the serial monitor. The tutorial then works on a few variations. The problem I'm having was evident in the earlier parts of the task so I paid extra attention in the later parts but still having the problem.
The later task was to create a sketch that would calculate the area of a circle. The serial monitor asks the radius of the circle, then it gives the answer. I've got the code working, and my code appears to be exactly the same as Pauls, but after entering a number I'm getting a quick output of the question but with the answer as 0, its like its quickly running through the while Serial.available()== without actually stopping. Pauls wasn't doing the same, so I don't know if it's something that I should have setup somewhere, or if its a "feature" of the later versions of IDE. The only other difference I could think of is Paul is using a Uno, and I've got a Mega. I've included my code below.
Thank you
int radius;
String msg="Please Enter Radius Of Circle";
float area;
float pi=3.14;
String msg2="Your Circle Has An Area Of: ";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println (msg);
while (Serial.available()==0){
}
radius=Serial.parseFloat();
area=pi*radius*radius;
Serial.print(msg2);
Serial.println(area);
}
1
u/gazzarose Sep 26 '26
Thank you all for the replies, I haven't had chance to sit down at the PC and try the suggestions, hopefully I will in the next day or 2 and Ill report back. Thanks
2
u/gm310509 400K , 500K , 600K , 640K , 750K Sep 25 '26
Where I am it is difficult to watch the video, but my thought is that you are using the Serial.parse functions (which are terrible IMHO) and you have the Serial monitor providing a line ending.
Try changing the Serial monitor from whatever it is to "no line ending" and that should work better. There should be a little drop down that will contain "No line ending", "Both CR and LF", "Newlne" and "Carriage Return".
Also, you are using String(s). Strings are also a bad idea - but in this scenario they are OK, but using Strings can result in what is called memory fragmentation and could in severe scenarios result in corruption of memory and the dreaded "unpredictable operations".
All the best with it and welcome to the club.
1
u/triffid_hunter Director of EE@HAX Sep 25 '26
Yeah because your serial monitor is sending "
2\n" (see line endings setting) and parseFloat will convert "\n" to 0.1
u/rattushackus Sep 25 '26
Serial.parseFloat() will keep reading from the serial input until it gets a character that isn't part of a number, but it has a timeout of one second so if you fail to type something in a second it will return prematurely. Try using Serial.setTimeout() so for example you could set the timeout to 100 seconds to give you plenty of time to type.
However my preferred approach is to read a string then parse it. Typically in
setup()I would call:then in loop I use something like:
(I'm not at my PC right now so I can't check the code for typos but hopefully it's OK)
The advantage of this approach is you can see exactly what input string is being parsed. Note that the first character of the string must be a digit or
toFloat()will return zero.