Code Snippet:
String a;
const int red_led_pin = 13;
void setup()
{
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
pinMode(red_led_pin, OUTPUT);
}
void loop() {
while (Serial.available())
{
a = Serial.readString();// read the incoming data as string
Serial.print(" First: ");
Serial.println(a);
if (a.equals("on"))
{
Serial.println("LED ON");
digitalWrite(red_led_pin, HIGH);
}
else if (a == "off" || a == "OFF")
{
Serial.println("LED OFF");
digitalWrite(red_led_pin, LOW);
}
Serial.print("Second: ");
Serial.println(a);
}
}
Serial.print
out:
Circuitry:
Issue:
The code can't capture the on
string or any other string (on
, off
or OFF
) that I pass. However it does pick it up and Serial.print
it.
What is going wrong?
Things I have tried:
I have tried as a comparison:
if (a.equals("on")){<>}
if (a == "on")){<>}
if (a.equalsIgnoreCase("on")){<>}
Due Diligence / Prior Research:
Serial.readString()
, so that the string doesn't equal"on"
, but for example `"on\n".Serial.readStringUntil('\n')