# Arduino RGB spectrum problem

Over the last few days I've been working on an anode RGB LED in Arduino, I've tried getting it to do an RGB spectrum but all I would get is just the colors from red to green, then it would get stuck on white and cyan, I believe that the red LED in the RGB Anode turns on and off, as the combination of green and blue is cyan and the three of them result white if they're equal which in this case I guess they are (not entirely sure).

Thing is the RGB Anode doesn't display all colors, I made it so the level of voltage given to the RGB anode is increased with a loop function and decreased once reached 255 with a decreasing function, here's the code:

    int redPin = 11;
int greenPin = 10;
int bluePin = 9;

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {

redFuncInc(1);
delay(100);
greenFuncInc(1);
delay(150);
blueFuncInc(1);
delay(200);

// calling decreasing methods

redFuncDec(255);
delay(10);
greenFuncDec(255);
delay(15);
blueFuncDec(255);
delay(20);

}

// Increasing from rainbow spectrum

void redFuncInc(int red){
for (int i = 1; i<=255; i +=1){

analogWrite(redPin, i);
delay(10);
}
}
void greenFuncInc(int green){
for (int z = 1; z<=255; z +=1){

analogWrite(greenPin, z);
delay(20);
}
}
void blueFuncInc(int blue){
for (int u = 1; u<=255; u +=1){

analogWrite(bluePin, u);
delay(30);
}
}
// Decreasing from rainbow spectrum

void redFuncDec(int red){
for (int y = 255;  y <=255;  y -=1){

analogWrite(redPin, y);
delay(10);
}
}
void greenFuncDec(int green){
for (int x = 255;  x <=255;  x -=1){

analogWrite(greenPin, x);
delay(10);
}
}
void blueFuncDec(int blue){
for (int g = 255;  g <=255;  g -=1){

analogWrite(bluePin, g);
delay(10);
}
}


• What is an "RGB Anode"? – Hearth Jul 4 '19 at 21:04
• I take that to mean a "Common Anode RGB" LED – vicatcu Jul 4 '19 at 21:10

(1) None of your Inc / Dec functions use the argument that is passed to them. Not sure if that was intentional, but it's noteworthy.

(2) Your Dec functions have a nonsensical termination condition in their loops. Instead of <=255 surely these should have >=0?

(3) The way you wrote your loop it's basically going to do the following sequence (after you fix (2) that is):

#000000 // BLACK
#010000
...
#FF0000 // RED
#FF0100
...
#FFFF00 // YELLOW
#FFFF01
...
#FFFFFF // WHITE
#FEFFFF
...
#00FFFF // CYAN
#00FEFF
...
#0000FF // BLUE
#0000FE
...
#000000 // BLACK


Not sure that's what you intended, but there you have it.

(4) Don't say z += 1 and z -= 1 in your for loops, say z++ and z--. People who read code will raise their eyebrows at the prior.

• Thanks for the quick reply though, how can I get it show colors like purple and pink and let it just complete the spectrum and keep on the loop? (I'm pretty new to Arduino) – Lintahlo Jul 4 '19 at 21:11
• Take a look at this: arduino.cc/en/Tutorial/ColorCrossfader – BB ON Jul 4 '19 at 21:13
• There's no canonical way to traverse the RGB spectrum. Color space is a complicated thing, but you might benefit from reading up on Hue Saturation Luminance color space and looking for code online that converts between HSL and RGB. – vicatcu Jul 4 '19 at 21:14