I have boiled this down to a very simple piece of code:
int testKey;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Input Source ID "); //Ask me the number
while (Serial.available()==0){ } //Wait for me to answer
testKey = Serial.parseInt(); //Store my answer in this variable
Serial.print("Parsed Input is "); //Print the results on serial monitor
Serial.println(testKey);
}
When I run the code on my Leonardo, or Uno, I expect to have Input Source ID to be printed on the serial monitor. It remains blank (it is set to 9600 baud). If I go ahead and enter a 1, then the serial monitor starts working. However, from this point on, it seems to ask me for the ID again, and print a 0 to the serial monitor:
Parsed Input is 1
Input Source ID
Parsed Input is 0
Input Source ID
It then is waiting for me to enter a new Source ID. If I then enter a 2, this is what I get, in addition to what was already there.
Parsed Input is 1
Input Source ID
Parsed Input is 0
Input Source ID
Parsed Input is 2
Input Source ID
Parsed Input is 0
Input Source ID
My question is, why doesn't the void loop start automatically and immediately prompt me for input? Secondly, after I give it a number, it seems to run it twice, inserting a 0 into the variable. Why? This seems so straightforward, but it has me stumped. I tried some delay statements here and there, to no avail. Thanks in advance.
-
You can set the line ending in the serial monitor to all combinations with carriage return and linefeed. Try all the possibilities and you see. No one of use uses the Serial.parse... functions, they are inconvenient.Jot– Jot2018年10月08日 20:03:55 +00:00Commented Oct 8, 2018 at 20:03
-
Ok, thanks to both of you! I have implemented your suggestions, and I am able to get the expected results. Kudos to you!bigmike333– bigmike3332018年10月08日 20:34:59 +00:00Commented Oct 8, 2018 at 20:34
2 Answers 2
My question is, why doesn't the void loop start automatically and immediately prompt me for input?
It does. However your serial monitor isn't open at that time and can't receive it. Unlike the Uno the Leonardo does not reset when you open the serial port.
A common workaround, to make it look more like an Uno, is to add:
while (!Serial);
in setup()
after Serial.begin(...)
, so that it does nothing until you open the serial terminal.
Secondly, after I give it a number, it seems to run it twice, inserting a 0 into the variable. Why?
Simple: you're parsing the number as a number. You're then parsing a blank line as the number.
If you have the serial monitor set to send CR + LF as the line ending you get the integer terminated by CR, followed by no integer (represented by 0) followed by LF.
This line:
while (Serial.available()==0){ } //Wait for me to answer
Doesn't wait for you to answer, it only waits until the first character is typed. This while loop happens about a million times a second on an Uno. You can't possibly type more than one character in that microsecond. Once you type it, the clock starts ticking, because parseInt()
uses a timeout value, so if no valid digit character is entered by the timeout, it exits. The default timeout is 1000ms (1 second).
-
1the entered value is sent from serial monitor after Send button or Enter key. Even at 9600 baud the gap between characters is far less then a second2018年10月08日 20:10:26 +00:00Commented Oct 8, 2018 at 20:10
-
True; I forgot about serial monitor.jose can u c– jose can u c2018年10月08日 20:12:08 +00:00Commented Oct 8, 2018 at 20:12
-
and you plan to leave it here?2018年10月09日 19:12:45 +00:00Commented Oct 9, 2018 at 19:12
-
Sure. If it's a poor answer, it will get downvoted, as it ought to be.jose can u c– jose can u c2018年10月09日 19:21:45 +00:00Commented Oct 9, 2018 at 19:21