void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() == 0) {}
Serial.print("I received: ");
Serial.println(Serial.parseInt());
}
This code wait until I send a number, - but it runs twice ... ?
Lets say i send 3:
Then the output looks like:
I received: 3
I received: 0
Why am I getting 2 lines for one "Send"?
How can I awoid to get the last line "I received: 0"
I assume you are sending from the serial monitor, what is line ending set to?
pylon
September 8, 2020, 5:09pm
3
This code wait until I send a number, - but it runs twice ... ?
No, it waits until the Arduino received a byte on the serial interface. It doesn't care if you send a number, a space, a carriage return or a new line. And the following code treats every byte sent as a number. A space will be a 0, a new line will be a 0, etc. I think you got the point.
I was thinking... a test sketch for situations like this:
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
Serial.print("I received: ");
Serial.println(Serial.read(), HEX);
}
}
When i try:
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
Serial.print("I received: ");
Serial.println(Serial.read(), HEX);
}
}
and send 3, I get:
I received: 33
I received: A
send 3, I get:
I received: 33
I received: A
Looks right to me. See reply #1 and #2 .
JCA34F
September 8, 2020, 5:43pm
7
At the bottom of serial monitor, set "line ending" to "No line ending".
Solved!
Thanks to you all.
I changed the serial monitor to "No line ending"
That solved the problem
Hex "A" is 000 1010 in this table, look it up, "LF" = line feed
A more user friendly list is here http://www.asciitable.com