RGB LED with serial monitor

I am trying to make an RGB LED light up a certain color when receiving a prompt from the serial monitor. My circuit is wired correctly because when I digital write each pin separately the light comes on. However, when I run the following and enter “red” I don’t get anything?

Any thoughts?

Thanks!

int red=8;
int green=9;
int blue=10;
String myColor;
String msg =“what Color Do You Want”;

void setup() {
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
}

void loop() {
Serial.println(msg);
while (Serial.available()==0){

}
myColor=Serial.readString();

if (myColor==“red”) {
digitalWrite(red,HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
}

if (myColor==“green”) {
digitalWrite(red,LOW);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
}

if (myColor==“blue”) {
digitalWrite(red,LOW);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
}
}

Try changing the line ending in the serial monitor to none.

1 Like

What is the line ending setting in the serial monitor. It needs to be set to None. If not the serial monitor sends a carriage return and/or line feed. "red\r\n " is not equal to “red”.

1 Like

That worked. I was not familiar with that setting. Thanks for your help!

That worked. I was not familiar with that setting. Thanks for your help!

@dalereesemay, your topic was moved to a more suitable location of the forum.

Can you in future please post code using code tags (three backticks as shown below)
```
your code here
```