Learning About RGB LEDS and IF Statements

I've been struggling trying to figure out why my code isn't activating the rgb LED.I have tested the it to make sure all the colors work but can't seem to activate anything in the serial Monitor Although the Prompt to turn on an led does come up and seem to work. Here's my CODE

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

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

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

you might have invisible characters in what you get

what's your line ending in the Serial monitor? try with "no line ending"

or

mycolor=Serial.readString();
mycolor.trim();

if (mycolor=="red"){
  ...

or

mycolor=Serial.readString();
mycolor.trim();

if (mycolor.startsWith("red")) {
  ...

so

const byte redPin = 10;
const byte greenPin = 9;
const byte bluePin = 8;

void setup() {
  // put your setup code here, to run once:
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(115200); // <<=== CHANGED, DON'T GO SLOW...
}

void loop() {

  Serial.println(F("What color do you want?"));
  while (Serial.available() == 0) yield(); // wait for something to appear
  String mycolor = Serial.readString(); // there will be a 1s timeout, code will feel slow to react. use setTimeout to adjust
  mycolor.trim();
  
  if (mycolor == "red") {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }
  else if (mycolor == "green") {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, LOW);
  }
  else if (mycolor == "blue") {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, HIGH);
  }
}

I would suggest to study Serial Input Basics to handle this in a better non blocking way

Gotcha this may have been from a tutorial that may be outdated was trying to follow it on youtube if you have any suggestions for Arduino tutorials for beginners it would be great to hear as I am still learning. Here's the link to that video if your interested Arduino Tutorial 20: Understanding RGB LED's - YouTube. Thanks

this guy seems he does not really know what he is doing but lots of joy :slight_smile:

as I said above, if you look at the bottom of his Serial monitor, you'll see

➜ No line ending

That worked thank you so much<3.

you have tutorials in the forum

You also have the built-in-examples https://docs.arduino.cc/built-in-examples/ and https://docs.arduino.cc/tutorials/

and the web is full of general examples, some better than others as your link highlighted

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.

He got very happy at 21:10
oh well.

Probably thinking of all the money he gets if people watch that far.

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