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);
}