Hello Arduino society. Recently I bought Arduino Uno from Aliexpress. I tried to copy sketchs from the book and strange things started to happen, I mean sketch gave different output. Can the problem was caused by Arduino Uno board, maybe guys from Chine made something different or is it just my stupid fault? Here is part of the sketch:
void loop(){
while (Serial.available()==0){}
code=Serial.parseInt();
Serial.println("OK");
}
Question is, why does "OK" appear two times on the serial monitor? should not it be just once? it appears two times and stops and waiting for input.
Thank you very much.
why does "OK" appear two times on the serial monitor? should not it be just once?
I assume that it appears when you enter something in the Serial monitor and press Return.
What have you got the Line ending set to in the Serial monitor ? If it is set to, say, newline, then the newline character will be appended to what you type so you could type one character, press Return and the Arduino will receive 2 characters.
If you have the line ending set to carriage return and line feed, the carriage return will be the trigger to end the parseInt() function, leaving the line feed in the buffer. Then, loop() runs again. Hey, there's stuff in the buffer, so parseInt() is called again. The linefeed is not a digit, so parseInt() ends, and the buffer is now empty. So, OK gets printed twice.
Print the value that parseInt() returned, to see what is actually being read from the serial port.
Better yet, don't use parseInt() at all. Reading and parsing serial data is an essential skill that you should develop.
Serial Input Basics