Hello
I'm unable to understand how is the default value getting printed in spite of spending an entire day to figure it out. Please help me to debug this program. I'm a newbie to Arduino by the way , so any help will be really appreciated by me.
int dt,num,i=1;
const int led=13;
void setup()
{
Serial.begin(9600);
pinMode ( led, OUTPUT);
delaytime();// Line 28
inblink();//Line 34
}
void loop()
{
blinking();
}
void blinking()// The blinking loop for the led
{
for (;i<=num;i++)
{
digitalWrite(led,HIGH);
delay(dt);
digitalWrite(led,LOW);
delay(dt);
}
}
void delaytime()//the delay timings of the led
{
Serial.println("Enter the delay time ");
while(Serial.available()==0){}
dt=Serial.parseInt();
Serial.println(dt);
Serial.flush();
}
void inblink()//accepts the number of times the led blinks
{
Serial.println("Enter how many times should the led blink");
while (Serial.available()==0){} // this is where I guess the problem is, plz help me
num=Serial.parseInt();
Serial.println(num);
}
And this is the output that I'm getting. I'm unable to enter how many times should the led blink. It's just automatically taking zero(0) even before I enter 5.
Hello Idahowalker
You'd asked what do I understand by
while(Serial.available()==0){}
Here's what I understand :
It checks for whether the serial buffer has any values ( which are entered by the user). If no value is entered, then "while" loops itself till the user enters some values.
It basically waits for the user to enter the values
I suppose, you have your 'line ending' settings set to CR / LF or both. After the first
dt=Serial.parseInt();
this is still in the read buffer and leads to a empty line in the second call which is interpreted as '0'.
You must empty the read buffer at the end of the reading:
void delaytime()//the delay timings of the led
{
Serial.println("Enter the delay time ");
while(Serial.available()==0){}
dt=Serial.parseInt();
Serial.println(dt);
while(Serial.available()!=0){Serial.read();}
}
Serial.flush();
will not help, because this is only relevant for the send buffer.
( This was different in very early versions of the IDE )
Hello MicroBahner
I don't know what struck me but I did 2 things reading your reply... please explain to me how did it work in both cases. I know nothing about it and I please beg of you to teach me about it. It will be very helpful to me. CASE 1
I just set the "line ending" as 'No Line Ending'. I got the expected result, I am very much happy about it though I don't know how it worked. Please teach me how it works CASE 2
I just followed your instructions and added the below statement to my program, and it also resulted in an expected successful result.
I didn't understand the above statement clearly. Please explain to me how it works and how does Serial.read() function just clears the read buffer.
I know this might be a bit time-consuming but please... anyone help me, I will be really grateful to you if you do explain me. Please
What is this thing called as "line ending" and how upon changing it did I get the correct values.
how does Serial.read() clear the read buffer instead of Serial.flush()
I just don't get what is being told. Please explain to me what above mentioned 2 things are, I read about read(), but I just understood that it accepts the char values from the serial monitor.
P.S. I've already got the expected result, but I'm very eager to learn about how it worked. And secondly Idahowalker, your referral links are quite helpful, thanks for it
Every character you send from the serial monitor is stored in a buffer at the arduino ( this buffer can hold up to 64 characters ). The Serial.available() function returns how many characters are stored in that buffer.
The Serial.parseInt(); function reads characters from the buffer sequentially and tries to convert them to an integer. If it reaches a 'non digit' character it stops the conversion and the result is returned. These 'digit' characters are deleted in the buffer.
Line ending characters are of course 'non digit' and they stay in the buffer. Therefor the next conversion starts with that line ending character, which means directly stopping the conversion and returning '0'.
The Serial.read() function reads and returns the next character in the buffer. The character is deleted in the buffer. So the while loop reads and deletes the characters in the buffer until its empty. The read characters are discarded, because the retun value is not used.
When no line ending character is send, the Serial.parseInt(); doesn't see the end of the number, because there is no 'non digit' character. But there is a timeout: By default it is 1 second. If there is no further character in the buffer for more than 1 second, it treats the number as fully received, and returns it. Therefore you see a short delay after sending your number in the monitor until it is printed. You don't have this delay if you send a line ending and delete this line ending character after receiving the whole number.
N.B. 'line ending' characters are special 'non printable' characters. They are e.g. used to start a new line when printing.
If you type input to a sketch in the serial monitor,
line endings can be used to distinguish between different lines of values/commands whatever.
You use the parsing commands to pick values from the stream as @MicroBahner explained.
Pressing enter or the send button can append characters to the entered data.
Your initial error rooted in not handling those characters,
so switching to none should have worked also.
What character(s) are appended, can be adjusted by a drop-down field in the serial monitor.