Skip to main content
2 of 2
Code formatting.
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

Reading a string from the serial port and comparing to another string

I'm using an Arduino Mega 2560 and writing a really simple piece of code that doesn't work.

String a, letra;
#define led 13

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

void loop() {
  while(Serial.available()) {
    letra= Serial.readStringUntil('\n');
    letra.trim();
    Serial.println(letra);

    if(letra.equals("aa\r\n")){
      Serial.println("ABRE 1");
    }
    if(letra == "aa\r\n"){
      Serial.println("ABRE 2");
    }
    if(letra.compareTo("aa\r\n") == 0){
      Serial.println("ABRE 3");
    }
  }
}

enter image description here

It's returning what I typed, but it's not entering any of the if conditions... Can someone help me with that ?