I’m trying to capture a string from the serial line, but having an issue.
If I send
xxx\r
yyy\r
zzz\r
I only want yyy in my “string”. Here’s the code I’ve been playing with. Can you help me figure out why string is xxxyyy???
#define SERIAL_SPEED 57600
#define LED 13
char charArray[256];
char c;
int i = 0;
int r = 0;
void setup()
{
pinMode(LED, OUTPUT);
charArray[0] = '\0';
// Set serial to 57600 bps
Serial.begin(SERIAL_SPEED);
Serial.println("Type something and hit enter, type some more and hit enter.");
delay(5000);
if(Serial.available() > 0)
{
while(Serial.available() > 0)
{
c = Serial.read();
if(c == '\r')
{
r++;
if(r <= 1)
{
continue;
}
else
{
break;
}
}
else if(c == '\n')
{
continue;
}
else
{
i++;
charArray[i-1] = c;
}
}
Serial.print("\"");
Serial.print(charArray);
Serial.println("\"");
}
digitalWrite(LED, HIGH);
}
void loop()
{
}