Hi, I am new to Arduino. I am using the Arduino MKR Zero board and trying to send and receive data to and from the board. Even though I am sending data only once it appears that my board is receiving it twice. Below is the simple code to test the serial data transfer:
void setup() {
Serial.begin(115200);
}
void loop() {
if(Serial.available() > 0){
// Reading data
char receivedChar = Serial.read();
// printing data
Serial.println(receivedChar);
// Making sure no data is left in Serial Input Buffer
while(Serial.available() > 0){
Serial.read();
}
}
}
When I send a character 'g' I always receive
g
g
in my Serial Monitor in all types, i.e. in 'No Line Ending', 'Newline', 'Carriage Return' and 'Both NL & CR'.
I tried reading about it but on all posts, everyone always mentions the to take care of Newline to get rid of such a problem. But I am doing it and still, I am getting two identical replies when I should be getting only one.
Also, to flush out the input buffer (as Serial.flush() is completely useless for this) I am adding the last part in my code, but this is also of no help. I am quite sure I am missing a small detail somewhere but I am unable to figure out where, can someone please help me regarding this?