I had to get a serial string on an arduino and read it back out.
This is what I got to work...but I am confused.
I am a little perplexed at the "else" statement...
1. Why do you need buf[count] = 0?
2. Doesn't doing the count++ make the next count equal to that? It works, but I thought it would turn the that value to 0 immediately.
3. Wouldn't this code always lead to buf[0] being 0? since it stores at buf[count++], wouldn't the first stored value be at buf[1]?
This is what I got to work...but I am confused.
Code:
char buf[MAX_MESSAGE];
char readS;
int count = 0;
while (Serial.available())
{
//read what is in buffer when something shows up
readS = Serial.read();
delay(5);
if (readS == '\r') {
Serial.print("You entered: ");
Serial.println(buf);
}
else {
if (count < MAX_MESSAGE-1) {
buf[count++] = readS;
buf[count] = 0;
}
}
}
1. Why do you need buf[count] = 0?
2. Doesn't doing the count++ make the next count equal to that? It works, but I thought it would turn the that value to 0 immediately.
3. Wouldn't this code always lead to buf[0] being 0? since it stores at buf[count++], wouldn't the first stored value be at buf[1]?