Why can't I use if else to change the color of the RGB led?

' I want to use if else to control the led but the led only shows red '

int red_light_pin= 11;
int green_light_pin = 10;
int blue_light_pin = 9;

String color, red, green, blue, ras, cyan, mage, yellow, white;

void setup() {
Serial.begin(9600);
Serial.println("Start");
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}

void LightMode()
{
Serial.println("Color: ");
while (Serial.available()==0) { }
color = Serial.readString();
Serial.println(color);

if (color == "red") RGB_color(255, 0, 0); // Red
else if (color == "green") RGB_color(0, 255, 0); // Green
else if (color == "blue") RGB_color(0, 0, 255); // Blue
else if (color == "ras") RGB_color(255, 255, 125); // Raspberry
else if (color == "cyan") RGB_color(0, 255, 255); // Cyan
else if (color == "mage") RGB_color(255, 0, 255); // Magenta
else if (color == "yellow") RGB_color(255, 255, 0); // Yellow
else if (color == "white") RGB_color(255, 255, 255); // White
else;
}

void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(red_light_pin, red_light_value);
analogWrite(green_light_pin, green_light_value);
analogWrite(blue_light_pin, blue_light_value);
}

void loop()
{
LightMode();
}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

——

Welcome to 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 </> icon above the compose window) to make it easier to read and copy for examination

if(color = red) RGB_color(255, 0, 0); // Red

Use = for assignment
Use == for comparison

= is for assigning a value
== is for comparison

The string you want to compare to is not the variable’s name… either you initialize those Strings with the right text or simply use

if(color == "red")

i tried and it doesn't work

Tks. But i tried and it doesn't work

J-M-L meant:

if(color == red)

Hello depua

For debugging purposes insert Serial.println() at POI to see what happens.

And what would that do?
red is a variable name of type String. Not a value inside that variable.
The String variables should be initialized properly before use.
Switch case may help to make your code clearer.

Did you tried to insert code with code tags?
Respect the forum rules, please

Hi everyone, I have implemented a function of RGB led by color with if/else structure, but when running, the light does not work.
I hope everyone can help.

int red_light_pin= 11;
int green_light_pin = 10;
int blue_light_pin = 9;

int inputValue = 0;
int outputValue = 0;

String color;

void setup() {
  Serial.begin(9600);
  Serial.println("Start");
  pinMode(red_light_pin, OUTPUT);
  pinMode(green_light_pin, OUTPUT);
  pinMode(blue_light_pin, OUTPUT);
}

void LightMode()
{
  Serial.println("Color: ");
  while (Serial.available()==0) { }
  color  = Serial.readString();
  Serial.println(color);
  
  if (color == "red") RGB_color(255, 0, 0); // Red
  else if (color == "green") RGB_color(0, 255, 0); // Green
  else if (color == "blue") RGB_color(0, 0, 255); // Blue
  else if (color == "ras") RGB_color(255, 255, 125); // Raspberry
  else if (color == "cyan") RGB_color(0, 255, 255); // Cyan
  else if (color == "mage") RGB_color(255, 0, 255); // Magenta
  else if (color == "yellow") RGB_color(255, 255, 0); // Yellow
  else if (color == "white") RGB_color(255, 255, 255); // White
  else;
}

void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);
}

void loop() 
{
  LightMode();
}

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.

How are you powering your project?
Have you written basic code to just test the operation of the RGB LEDs?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

What gets printed when you enter red ?

Serial.println(color);

And led don't change

I have checked the lights work properly

If that is a common cathode LED wire it up as in circuit H3 below:

What is the line ending in Serial Monitor set to? Readonably sure that it's cr/lf. So e.g. "red" is not "red" but "red\r\n" and hence the compare fails.

Set the line ending to none in SerialMonitor is one solution.

@depua
add the line

color.trim();

after reading the color from Serial

Where are the series resistors for the RGB led?