/ / Comunicazione seriale su Arduino - arduino, comunicazione

Comunicazione seriale su Arduino - arduino, comunicazione

Ho un incarico per la scuola dove devoaccendere un led con il messaggio seriale # ON% e spegnere il led con # OFF%. # E% sono gli identificatori per la stringa corretta. Quindi ho creato questo codice:

(bericht significa messaggio in olandese)

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 = "";
}
}
}

Il problema è che il programma non entrerà mai nella sezione if (bericht == "# ON%") ...

Scusami se questa è una domanda sciocca ma con un sacco di ricerche su Google non riesco proprio a capirlo ...

risposte:

2 per risposta № 1

Il problema è qui:

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

questo in realtà aggiunge un numero intero al messaggio. Rimuovi il +:

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