If sentence does not detect a string

I am doing a command and that can be sent through the serial monitor
the problem is that when I want to detect it through an if nothing happens and the loop starts again

void setup() {
  Serial.begin(9600);

}

void loop() {
  Serial.println("Introduce el comando");
  while(Serial.available()==0){}

  String i = Serial.readString();
  i = "Hola";
  if(i == "Hola"){
      Serial.println("Como estas");
      digitalWrite(13, LOW);
    }
}

If line endings in the serial monitor is not set to No Line Ending a line feed and/or carriage return are sent with whatever is entered. “Hola\r\n” is not equal to “Hola”. Set line endings to “No Line Ending” and try your code after commenting out the

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

void loop()
{
   Serial.println("Introduce el comando");
   while (Serial.available() == 0) {}

   String i = Serial.readString();
   //i = "Hola";
   if (i == "Hola")
   {
      Serial.println("Como estas");
      digitalWrite(13, LOW);
   }
}

There are better ways to receive serial data outlined in the serial input basics tutorial.

1 Like

Thank you man

i.trim()
will make this work with any line ending

what does that do?

trim() removes leading and trailing whitspace
spaces, tabs, newlines, carrageReturns, vertical tab and form feed.
so if your readString() reads in a newline or NL&CR, trim() will remove it before you try
i == “Hola”
If you are reading in text check out my
Arduino Software Solutions which has number of sample sketches for reading from Serial with their pros and cons.
Also if you are using Strings check out my tutorial on
Taming Arduino Strings

Thanks, I’ll see it