# Help controlling color and brightness of RGB LED's.....

Hey, experienced programmer, but Arduino/electronics noob here. Need a bit of help with my first real project.

I'm trying to rig up a halloween mask with 2 RGB LED's to light up the eyes. I want to be able to control the color with 3 pots (1 for R, 1 for G, 1 for B), and another pot to control the brightness.

Here's my sketch for controlling the color via the 3 (RGB) pots:

``````// set pins for LEDs
const int R1 = 3;
const int G1 = 5;
const int B1 = 6;
const int R2 = 9;
const int G2 = 10;
const int B2 = 11;

// set pins for pots
const int R = 0;
const int G = 1;
const int B = 2;
const int BRIGHT = 3;

void setup()
{

}

void loop()
{
r = map(r, 0, 1023, 0, 255);

g = map(g, 0, 1023, 0, 255);

b = map(b, 0, 1023, 0, 255);

analogWrite(R1, r);
analogWrite(G1, g);
analogWrite(B1, b);
analogWrite(R2, r);
analogWrite(G2, g);
analogWrite(B2, b);
}
``````

Basically, this just reads the pot value for each color and remaps it to 0-255, then writes out the values to the LEDs. Hopefully this is the correct approach but please let me know if not!

Also, whatever color the LEDs are, I want to use another pot to control the brightness. I've read that using HSV values is the way to go with this, and I found a converter for RGB>HSV here: http://www.easyrgb.com/index.php?X=MATH&H=20#text20, but I dont really understand what's going on enough to really know how to implement it in my own code.

So my question is how do I implement that conversion function properly, including using the value of the brightness pot to alter the resulting HSL value in order to alter the overall brightness? And finally, I'm assuming I would just convert from HSL back to RGB and write out the values to the LED to apply it, is that correct?

Thanks so much in advance for any advice, I really appreciate any help! =]

So you want to start with RGB, Convert to HSV, then adjust brightness dependent on pot position, then convert back again, to send it to your LEDS?

I'd think the solution would be simpler than that. The positions of the RGB knobs define the ratio of the different colours, then the Brightness control will scale them upto maximum. eg

``````// set pins for LEDs
const int Red1 = 3;
const int Green1 = 5;
const int Blue1 = 6;  //THE VAR NAME B1 IS ALREADY TAKEN
const int Red2 = 9;
const int Green2 = 10;
const int Blue2 = 11;

// set pins for pots
const int R = 0;
const int G = 1;
const int B = 2;
const int BRIGHT = 3;

void setup()
{

}

void loop()
{

brightness = map(brightness, 0, 1023, 0, 255);

r = map(r, 0, 1023, 0, brightness);
g = map(g, 0, 1023, 0, brightness);
b = map(b, 0, 1023, 0, brightness);

analogWrite(Red1, r);
analogWrite(Green1, g);
analogWrite(Blue1, b);
analogWrite(Red2, r);
analogWrite(Green2, g);
analogWrite(Blue2, b);
}
``````

I guess sometimes you just need a fresh perspective =] Still making the adjustment from software to hardware programming, but that makes a lot of sense, thanks for simplifying my workflow! And thanks for the tip on the B1 variable as well, I'll adjust accordingly.

Just for the record, the reason I read HSV was the way to go was because it made sense that not all RGB values can simply be lowered or raised to alter their brightness. By doing that you may completely remove the subtle amount of red in the hue, for instance. So converting to HSV would let me simply alter the V portion, which I understood translated to brightness. So thats why I figured Id alter than, then convert back to RGB so I can feed the proper values to the LEDs.

Obviously that was the long way, so Im grateful for the shortcut!

Thanks again, cant wait to get home tonight and try it out!

blkhwks19: Just for the record, the reason I read HSV was the way to go was because it made sense that not all RGB values can simply be lowered or raised to alter their brightness. By doing that you may completely remove the subtle amount of red in the hue, for instance. So converting to HSV would let me simply alter the V portion, which I understood translated to brightness. So thats why I figured Id alter than, then convert back to RGB so I can feed the proper values to the LEDs.

I'm aware of that. This is why there are twice as many Green sensors on cameras than the other two. (to emulate the human eye more closely). There are also several algorithms around to convert colour to monochrome. None of them give equal weight to red green and blue. Also, I very much doubt that there's a linear corelation between power and light intensity for any LED. We can but try eh?

Hopefully by NEXT haloween you'll have it just right ;)

Hopefully by NEXT haloween you'll have it just right smiley-wink

I'd better, cuz I've got more grand ideas for next year! Didn't want to overwhelm myself this year, so some simple lights with controls was a good start and will still seem impressive, given the relatively short time frame I have to work before Halloween.

Can't wait for next year though....