I have the following code:
int count = 0;
char serial_char;
char serial_message[96];
void setup(){
Serial.begin(9600);
}
void loop(){
while(Serial.available() > 0){
serial_char = Serial.read();
if (serial_char == '\n' || serial_char == '\r'){
serial_message[count] = 0; // <<-- What does this line do?
count = 0;
Serial.println(serial_message);
}
else{
serial_message[count++] = serial_char;
}
}
}
With this code, I can type a message into the serial monitor, and it will print the message back at me. So if I type 'Goodbye', it returns:
Goodbye
If I send it another message 'Word', it returns:
Word
However, my questions is when I comment out the line:
serial_message[count] = 0;
And then I repeat the same sequence of actions before, 'Goodbye' returns 'Goodbye', but when next I enter 'Word', it returns:
Wordbye
Could anyone explain this to me? I am really curious to know why this occurs. What does adding that 0 in the char array accomplish? Thank you.