Serial.readString()

Hi, I would like to use my serial input to test my program using texts.
For instance, if I type the text "demo" in my serial monitor input, my program has to check this and then do something with it.

I am using Serial.readString() to get the input text. But if I use if to check the text that I enter, I alway get to see: "you started the demo routine".

What am I doing wrong?

Here's the code:

String incoming = "";   // for incoming serial string data

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

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming:
    incoming = Serial.readString();
    // say what you got:
    Serial.println(incoming);    
    if (incoming == 'demo') {
      //demo routine  
      Serial.println("you started the demo routine");
    } 
    else if (incoming=='sort') {
      //sorteer routine
      Serial.println("you started the sort routine");
    }     
    else {
      //junk
      Serial.println("something else");
      incoming = "";
    } 
    Serial.flush(); 
  }  
}

Clear your incoming in e.g. the beginning of loop.

Read up on the difference between single quotes and double quotes; all your if statements should use double quotes.

Consider getting rid of the String class (capital S) and e.g. use one of the approaches as demonstrated in Updated serial input basics which use c-strings (null terminated character arrays).

if (incoming == 'demo')

' go round single characters. " go round Strings

What have you got the line ending set to in the Serial monitor ?

'demo')

Single quotes for single characters. Double quotes (" ") for Strings.

I would suggest that you not use String objects unless you know the pitfalls.

The methods in serial input basics allow you to receive serial data into a null terminated character array (c-string). Further processing can be done with the c-string functions.

This code doesn’t work with serial monitor, arduino ide ,any fix ?

ajyo:
This code doesn't work with serial monitor, arduino ide ,,any fix ?

Which code would that be in this 8 month old thread ?
Please post the code here or maybe start a new thread