/ / Komunikacja szeregowa na Arduino - arduino, komunikacja

Komunikacja szeregowa na Arduino - arduino, komunikacja

Mam zadanie do szkoły, gdzie muszęwłącz diodę LED z komunikatem szeregowym # ON% i wyłącz diodę z # OFF%. Liczba i% to identyfikatory poprawnego łańcucha. Zrobiłem ten kod:

(bericht oznacza wiadomość w języku niderlandzkim)

String readString = "";
int recievedCharacter;
String bericht = "";

int ledPin = 6;


void setup() {
Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
pinMode(ledPin, OUTPUT);
}

void loop()
{

while (Serial.available() > 0)
{
delay(4);
char readChar = (char) Serial.read(); // "Convert" to needed type
bericht += + readChar;         // concatenate char to message

}

if(bericht.startsWith("#"))
{

if(bericht == "#ON%")
{
Serial.println(bericht);
Serial.println("goed");
digitalWrite(ledPin, HIGH);
//message = "";
}



if(bericht == "#OFF%")
{
Serial.println("goed");
digitalWrite(ledPin, LOW);
//message = "";
}
}
}

Problem polega na tym, że program nigdy nie przejdzie do sekcji if (bericht == "# ON%") ...

Przepraszam, jeśli to głupie pytanie, ale z dużą ilością googlowania, po prostu nie mogę tego zrozumieć ...

Odpowiedzi:

2 dla odpowiedzi № 1

Problem jest tutaj:

bericht += + readChar;         // concatenate char to message // XXX "+ char" => int

to faktycznie dodaje liczbę całkowitą do wiadomości. Usunąć +:

bericht += readChar;         // concatenate char to message // Goed!