Author Options:

How do I make a RGB leg fade between colors using a arduino? Answered

I would like to make a RGB led that fades inbetween colors how do I do that? Do I just do the fade code 3 times but just put in the 3 different ports in which the different color thing are in? Also do I make 3 different fade circuits from the arduino site?


The forums are retiring in 2021 and are now closed for new topics and comments.

8 years ago

Maybe and yes and no.

You asked a lot of non-specific questions there.

To make rgb, you need to fade in r,
then fade out r as you fade in g,
then fade out g as you fade in b,
then fade out b (or fade back to red).

This can be accomplished with something like:
(declare led pins as int redLed= bla, greenLed = blabla, blueLed = whatever pin you use to hook up the leds, must be pwm pins)
Be sure to hook up the leds in the correct orientation with a resistor. (same as the fade led example, but 3 of them on different outputs)
Note the sneaky trick of using one for loop but calculating multiple output values from i, by either assigning the value of i, or subtracting i from 255 to get the 'opposite' number.

The delay is in there to make it a bit slower. This should take 2550 milliseconds to complete one transition, and about 7.5 seconds to finish the whole rotation and start at the top again. Try messing around with the value in the delay.

void loop(){
for (i = 0, i <256, i++){ //count i from 0 to 255 inclusive
analogWrite(redLed,i); //ramp up red
analogWrite(blueLed,255-i); //ramp down blue
for (i = 0, i <256, i++){
analogWrite(greenLed,i); //ramp up green
analogWrite(redLed,255-i); //ramp down red
for (i = 0, i <256, i++){
analogWrite(blueLed,i); //ramp up blue
analogWrite(greenLed,255-i); //ramp down green