if loop won't check two strings?

Hey,
I’m trying a simple thing for a project and I can’t figure out what I’m doing wrong.
I want to make the led on the board blink when I type ‘Blink’ in the Serial Monitor.
When I check the two thing it need to compare, it gives exactly the same, but it still won’t make the led blink. Does anyone know what I have to change in my code?
Thanks in advance!

P.S. I put my file as an attachment to this topic.

State_Blink.ino (462 Bytes)

Here's the code he attached.

int lightPin = 13;
String state;
String Blink;


void setup() {
  Serial.begin(9600);
  pinMode(lightPin, OUTPUT);
  state = String("Rest");
  Blink = String("Blink");
}

void loop() {
    if (Serial.available()>0)
    {
      state = Serial.readString();
      Serial.print(state);
      Serial.print(Blink);
    }
    if (state ==Blink)
    {
      digitalWrite(lightPin,HIGH);
      delay(1000); 
      digitalWrite(lightPin,LOW);
      delay(1000);
    }
}

Add this:

 Serial.print(state ==Blink);

After your other print statements and you should see that the code does work only if the serial monitor is set to "No line ending" on the bottom right.

The String class is very powerful, but most of the time you do not use all of that power. As a result, your code size is bigger than it needs to be. Your program compiles to 4482 byte. By getting rid of the String objects and using C strings instead, the code size drops to 2646:

int lightPin = 13;
char state[] = "Rest";
char Blink[] = "Blink";


void setup() {
  Serial.begin(9600);
  pinMode(lightPin, OUTPUT);
  //  state = String("Rest");
  //  Blink = String("Blink");
}

void loop() {
  static char buffer[6];
  int charsRead;
  
  if (Serial.available() > 0)
  {
    charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
    buffer[charsRead] = '\0';     // Now it's a string    
    Serial.print(state);
    Serial.print(Blink);
  }

// if (strlen(buffer) == sizeof(Blink) - 1)   // Less rigorous
  if (strcmp(Blink, buffer) == 0) 
  {
    digitalWrite(lightPin, HIGH);
    delay(1000);
    digitalWrite(lightPin, LOW);
    delay(1000);
  }
}

If you uncomment the if statement block, the condition becomes true on any string that is 5 characters in length, which makes it less demanding as an input check. That may be good or bad, depending upon your needs.

@Blue Eyes Thank you for your note.

After your other print statements and you should see that the code does work only if the serial monitor is set to "No line ending" on the bottom right.

It indeed worked for me. Can you perhaps tell me what the influences of the options in the serial monitor are (sorry I'm just new in C++)?

@econjack Thanks for your remarks! Luckily memory doesn't matter to my project, but I'll make the changes!

Again thanks to all of you!

If the serial monitor is set to Newline then a (non visible) newline character is added to what you type in. Look at the length of the string that you type in and it will be one more than the number of characters. In your example print the length of the state variable when "Blink’ is entered. With “No line ending” selected and it is 5. switch to Newline and the length is 6.

Aha, thx. And of course that would be the reason that it wouldn't see the two stages as the same. Thanks a lot!