Compare a string from serial input

A string that is coming from the serial port has te match a certain value.
But it never appears to be .
here you see my test script. Why does it not work whan i hive "hello" in the serial?


void setup() {
  Serial.begin(115200);
}

void loop() {
   if (Serial.available() > 0) {
   String c = Serial.readString();
 
   if (c == 'hallo') {
    
     Serial.print("Good!");

} else {Serial.print("Bad!");
}}}

Welcome to the forum

Why did you start a topic in the Uncategorised category of the forum ?

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

Single quotes for single characters ('t'). Doube quotes for Strings ("hallo").

Also be aware of line endings appended by serial monitor.

try String c = Serial.readStringUntil(0x0A);

void setup() {
  Serial.begin(115200);
  Serial.println("Start");
}

void loop() {
  if (Serial.available() > 0) {
    String c = Serial.readStringUntil(0x0A);
    Serial.println(c);

    if (c == "hallo") {
     
      Serial.print("Good!");

    } else {
      Serial.print("Bad!");
    }
  }
}

Try it like this..

Thank you for your help, for the simulation below this helps.
For the actual application i try to make, not yet. My arduino is connected to a pc running this software
it send the serial code “9999\r” by plain ASCII. As a response tha arduino has to send a sting to identify itself. Once I could have it sent "9999" back (lost that code). since than i only get a timeout.

It is meant as a challenge but i haven't made it one step so far as i fail this simple communication protocol :frowning:

\r is return character in hex 0x0D..
\n is newline character in hex 0x0A..
did you try reading until 0x0D instead of 0x0A??

good luck.. ~q

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.