My code doesnt whant to run:(

Hi.
I am learning how to code Arduino, but one of my first "projects" doesn't work. I'm working with an RGB LED and have connected it correctly, but there is something wrong with my code please help. This is my project: It looks at the serial monitor to see what color you have written and then lights the LED in that color, Here is my code

int redPin=8;
int greenPin=9;
int bluePin=10;
String myColor;
String msg="What color do you want?";

void setup() {
Serial.begin(9600);
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"){
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);

}
if (myColor=="green"){
digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,LOW);

}
 if (myColor=="blue"){
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,HIGH);

}
if (myColor=="off"){
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);

}
}

  

Try changing the serial monitor line ending to "no line ending"

Quite a descriptive account of the issue.

Or try changing:
if (myColor=="string"){
to
if (myColor.startsWith("string")){

1 Like

Thank you so much

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.