RGB will only light up with a != and not == string

I have an RGB that works if I connect 5v directly to each leg. But when programming from input from monitor, it does not execute any code within {} light up unless I put != in the if statement. here's the code

int redPin=2;
int greenPin=4;
int bluePin=6;
String msg="Choose a color";
String myColor;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}

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

if (myColor=="red"){
Serial.print(myColor);
analogWrite(redPin,255);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
}

if (myColor=="green"){
Serial.print(myColor);
analogWrite(redPin,0);
analogWrite(greenPin,255);
analogWrite(bluePin,0);
}
if (myColor=="blue"){
Serial.print(myColor);
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,255);
}

}


I've changed arduino boards, pins, breadboards. This is exact code that a guy on YouTube was showing, and I can't get my project to repeat it. Please help

The only way if lights up and executes any code, is if I enter

if (myColor!="blue"){

then it prints the color, and lights up

What is the serial monitor line ending?

The serial monitor just loops through the prompt of:

Choose a color
Choose a color

(One line per entry and ht enter or send)

====================================

Then,

When I change the line:

if (myColor!="red"){

to add the != statement, then the output is:

The serial monitor shows:

Choose a color
red
Choose a color
blue
Choose a color
anything I type in
Choose a color
test
Choose a color

each time I put an entry and hit Enter or send.

What is the the serial monitor line-ending control set to?

Hint: Look at the bottom of the serial monitor window...

The line-ending control is set to Newline.

I can directly light up any led color with a analogWrite(redPin,255) or pinning directly to 5v or 3.3v

Update:

I changed the if statement to an else-if statement and posted the code below. Now when I type in anything (red, green, blue or jibberish) it lights up the blue led for 2 seconds.

int redPin=2;
int greenPin=4;
int bluePin=6;
String msg="Choose A Color";
String msg2="You Picked ";
String myColor;
int dt=4000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:

Serial.println(msg);
while (Serial.available()==0){
}
 myColor=Serial.readString();

if (myColor=="red"){
  analogWrite(redPin,255);
  analogWrite(greenPin,0);
  analogWrite(bluePin,0);
  delay(dt);
  analogWrite(redPin,0);
}
else if (myColor=="green"){
  analogWrite(redPin,0);
  analogWrite(greenPin,255);
  analogWrite(bluePin,0);
  delay(dt);
  analogWrite(greenPin,0);
} 
else if (myColor=="blue"); {
  analogWrite(redPin,0);
  analogWrite(greenPin,0);
  analogWrite(bluePin,255);
  delay(dt);
  analogWrite(bluePin,0);
}
}
else if (myColor=="blue");

The ; at the end of the statement is an error. Remove it. See Trap #13.

http://www.gammon.com.au/tips

The line-ending control is set to Newline.

Earlier posters were trying to lead you to the realization that "red\n" is not going to be equal to "red" when compared.

String comparisons are very particular about line endings which are part of the String.

I retyped the code and errantly put the ; at the end of the bluePin line. Thanks for picking oup on that.

This did not make any difference in the lack of execution with this code.

My serial monitor is set to Newline.

Really confusing as the guy i watched online (older video) did the exact code, and it worked like a charm. He used a single rgb led and I had 2 of the exact types on hand and connected everything verbatim and coded. His worked but mine did not.

Running 1.8.12 UNO R3 and connected to a Windows 10 PC

There's something happening that i cannot see though. Since putting in the delay, it takes a few seconds to complete a loop, then prompts for the next command. Same result - with or without the delay, and changing the name of variable, changing pin locations. I can directly code to run an led to light up, but not through the Serial.readString without putting a 'not equal' equation in there.

Really frustrating and obviously reaching out for help from peeps that are more well versed in coding the Arduino.
Thx

My serial monitor is set to Newline.

At the bottom of the serial monitor window where you have set set the newline ending in the pull down menu, select no line ending instead.

Problem Solved

Thanks "cattledog" your instruction worked. I had the serial monitor set to Newline but once changing it to on line ending, it worked as expected.

This is one for the books! I will ensure this is the setting for here on out. I may resume my learning. Thx.

woodkraftz:
I will ensure this is the setting for here on out.

As long as you are just playing around, you could. But "real life" human serial interaction is line oriented, where you type and edit a line if desired with backspace, and then submit it with the "enter" or "return" key. The serial monitor in Arduino is just a debugging tool. It was never meant to be used to really control a sketch. For a more comfortable, intuitive interface use a terminal emulator like Putty (there are many others, I use Tera Term). You can then design your program to accept and process newlines normally.

Thanks for the update. I do have PuTTY but never thought about interfacing that. What would be the arduino 'hook' to get the comms going?

woodkraftz:
Thanks for the update. I do have PuTTY but never thought about interfacing that. What would be the arduino ‘hook’ to get the comms going?

Start with the “Serial input basics” examples linked to in the sticky threads at the top of the forum.