-1
#include <stdio.h>
#define LED 13
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() == 4) {
char command[5];
for (int 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");
}
}
}
나는 4 자 길이의 문자열을 읽습니다. 그러나 4 자 수가 아닌 문자열은 무시해야합니다.Arduino 클리어 버퍼
그래서,이 입력을 상상 :
AAAA
BBBB
BBB
AAAA
지금은 읽고 { "AAAA", "BBBB", "BBBA"}.
나는 { "AAAA", "BBBB", "AAAA"}를 읽어야합니다.
아이디어가 있으십니까? 고맙습니다.