Help with this code please

Thank you guys for being here. I am just getting started and did some lines and they do not work. I have tested the LEDs by going to 5v direct on the board and they light up so it has to be in the code.

int RED=7;

int GREEN=8;

int BLUE=9;

String myColor;

String msg="What Color LED? ";

void setup() {

// put your setup code here, to run once:

Serial.begin (9600);

pinMode (RED,OUTPUT);

pinMode (GREEN,OUTPUT);

pinMode (BLUE,OUTPUT);

}

void loop() {

// put your main code here, to run repeatedly:

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

}

}

Welcome to the forum

Your topic has been moved to the Programming Questions category of the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

where you send color message to arduino from ? serial monitor ? make picture of that window (whole window)

  • Have you tried printing myColor before testing its value ?
  • Have you got current limiting resistors in series with the LEDs as you should have ?
  • What baud rate is your Serial monitor set to ?
  • What Line Ending setting do you have in the Serial monitor ?

Incidentally, your code works OK on my test system

Try this line:

This way:

myColor = Serial.readStringUntil('\n');

Thank you will give it a try.

the serial monitor needs to be set to "No line ending" otherwise when you type red and then enter, the stringis "red\n"

Here is a YouTube video that does almost exactly what you are trying. As @gcjr said it is very important to set to “no line ending”. Also upper and lower case are handled. Worth the watch. It will almost certainly get your project working.

Thank you, mostly got it working the red and blue work but not the green. I will see if I can figure it out.

Ok got it working here is what fixed it.
myColor=Serial.readStringUntil('\n');

each color looked like this

if (myColor=="red"){
digitalWrite (RED,HIGH);
digitalWrite (GREEN,LOW);
digitalWrite (BLUE,LOW);

Progress is good. Keep plugging away.