#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 character string with Arduino Serial, and when this AAAA turns on the LED, when it is BBBB, turn off the serial port.
However, when I enter "AAAA", it reads "AAAÿ" with a lot of "ÿ" along the way.
I think I read everything correctly, but it doesn’t work so well, any idea what I am doing wrong?
source
share