Arduino read string from Serial

arduino, c

Solution

#define numberOfBytes 4
char command[numberOfBytes];

    void serialRX() {
      while (Serial.available() > numberOfBytes) {
        if (Serial.read() == 0x00) { //send a 0 before your string as a start byte
          for (byte i=0; i<numberOfBytes; i++)
            command[i] = Serial.read();
        }
      }
    }

Problem

``` #include <stdio.h> #define LED 13 void setup() { pinMode(LED, OUTPUT); Serial.begin(9600); } void loop() { int i; char command[5]; for (i = 0; i < 4; i++) { command[i] = Serial.read(); } command[4] = '\0'; Serial.println(command); if (strcmp(command, "AAAA") == 0) { digitalWrite(LED, HIGH); Serial.println("LED13 is ON"); } else if (strcmp(command, "BBBB") == 0) { digitalWrite(LED, LOW); Serial.println("LED13 is OFF"); } } ``` I am trying to read a 4 characters long string with Arduino's Serial, and when it is AAAA turn on a LED, when it is BBBB turn off the serial. However, when I enter "AAAA" it reads "AAAÿ" with lots of "ÿ"'s along the way. I think I'm reading everything correctly, but it's not working so well, any idea of what I'm doing wrong?

Original source