user85938: New to Arduino, getting into String variables and reading them from serial monitor.
Basically what I'm trying to do is have the user input either red, green, yellow. Whichever String they enter the LED comes on.
What is happening seems to be that the if statements are not true and are never entered thus not LEDs come on. I do have a serial.Println(myColor) at the end which displays myColor as the user has inputted into serial monitor. I just don't understand why the if statements are not firing. its really weird.
String msg="What LED do you want to turn on?";
String myColor;
int redPin = 12;
int greenPin = 13;
int yellowPin = 8;


void setup(){
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}

void loop(){
    //LED ColorDEmo

    Serial.println(msg);                //print the question to serial monitor
    while(Serial.available() == 0){     //while we wait do nothing till data is available
    
    }
    myColor = Serial.readString();      // set myColor to serial value
    
   
   

    if (myColor == "red"){              // if the value is red
        Serial.println("In red");           
        turnOffLEDS();                  // turn off all LEDS
        digitalWrite(redPin, HIGH);     // turn on red lED
    }

    if (myColor == "green"){            // if the value is green
        Serial.println("In green");
        turnOffLEDS();                  // turn off all LEDS
        digitalWrite(greenPin, HIGH);   // turn on green LED
    }

    if (myColor == "yellow"){           // if the value is yellow
        Serial.println("In yellow");
        turnOffLEDS();                  // turn off all LEDS
        digitalWrite(yellowPin, HIGH);  // turn on yellow LED
    }
    Serial.println(myColor);            // print myColor
   
}


// turns all LEDS off
void turnOffLEDS(){
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
}


user16780: Avoiding String objects (back to the basics) often makes life easier.
Simply check if the answer contains one of the characters 'd' 'g' 'y'
Or (and) simplify your protocol to only send one character.
BTW:You might notice a delay in readString, waiting for a multi-character input to finish, because Serial.available() is true already after the first character of an input, and readString terminates only if it times out

85938: <p>New to Arduino, getting into String variables and reading them from serial monitor.
Basically what I'm trying to do is have the user input either red, green, yellow. Whichever String they enter the LED comes on.</p>
<p>What is happening seems to be that the if statements are not true and are never entered thus not LEDs come on. I do have a serial.Println(myColor) at the end which displays myColor as the user has inputted into serial monitor. I just don't understand why the if statements are not firing. its really weird.</p>
<pre><code>String msg=&quot;What LED do you want to turn on?&quot;;
String myColor;
int redPin = 12;
int greenPin = 13;
int yellowPin = 8;


void setup(){
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}

void loop(){
    //LED ColorDEmo

    Serial.println(msg);                //print the question to serial monitor
    while(Serial.available() == 0){     //while we wait do nothing till data is available
    
    }
    myColor = Serial.readString();      // set myColor to serial value
    
   
   

    if (myColor == &quot;red&quot;){              // if the value is red
        Serial.println(&quot;In red&quot;);           
        turnOffLEDS();                  // turn off all LEDS
        digitalWrite(redPin, HIGH);     // turn on red lED
    }

    if (myColor == &quot;green&quot;){            // if the value is green
        Serial.println(&quot;In green&quot;);
        turnOffLEDS();                  // turn off all LEDS
        digitalWrite(greenPin, HIGH);   // turn on green LED
    }

    if (myColor == &quot;yellow&quot;){           // if the value is yellow
        Serial.println(&quot;In yellow&quot;);
        turnOffLEDS();                  // turn off all LEDS
        digitalWrite(yellowPin, HIGH);  // turn on yellow LED
    }
    Serial.println(myColor);            // print myColor
   
}


// turns all LEDS off
void turnOffLEDS(){
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
}
</code></pre>
 16780: <p>Avoiding String objects (back to the basics) often makes life easier.
Simply check if the answer contains one of the characters 'd' 'g' 'y'</p>
<p>Or (and) simplify your <em>protocol</em> to only send one character.</p>
<p>BTW:You might notice a delay in readString, waiting for a multi-character input to finish, because Serial.available() is true already after the first character of an input, and <a href="https://www.arduino.cc/reference/en/language/functions/communication/serial/readstring/" rel="nofollow noreferrer">readString terminates only if it times out</a></p>
