Hi. This in my loop:
int val = Serial.read();
Serial.println(val-'0');
I expect the Arduino to send nothing to my computer but Serial Monitor keeps receiving -49 although no key was pressed and mirrored. Could anyone please explain? Thank you
Hi. This in my loop:
int val = Serial.read();
Serial.println(val-'0');
I expect the Arduino to send nothing to my computer but Serial Monitor keeps receiving -49 although no key was pressed and mirrored. Could anyone please explain? Thank you
You are receiving an integer 0xFFFF, or -1. Subtract 48 from -1 equals -49.
Use Serial.available() to determine if there are characters in the Serial buffer.
edit: A return of -1 indicates no characters in the serial buffer.
edit: A return of -1 indicates no characters in the serial buffer.
And -1 - 48 is -49. So, stop reading when there is not data to read.
Ahhh.. what? The arduino is sending data to tell me nothing has been received? Why? And is there a way to disable this annoyance? Thank you
Just use a:
if (val != -1)
ok that solves it. Thank you.
But still, out of curiosity, why does the Arduino send data without me telling it to? I would like to be in control of what it sends.
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
...R
why does the Arduino send data without me telling it to?
You DID tell it to. That is what Serial.print() does.
The Arduino did NOT get data. That is what the -1 is telling you.