# RGB LED - Fade Between Random Colors

I am trying to generate a random color RGB value -> light up RGB led -> wait for 5 minutes -> generate next random color -> fade from old color to new color.

• For showing the color on LED I am using 3 channel PWM output on ATMEGA8.
• For generating the random color I am using TIMER0 as seed value and then generating a random value according to formula return (unsigned char)(rand()/(RAND_MAX/255 + 1)); for the 3 channels.

Now since I want to show the intermediate colors as well (using PWM), here is what I am thinking of doing:

• total transition steps = 500
• time to show each intermediate color = 5ms
• redStep = (finalred - initalred)/500 (same for green and blue)
• for loop (1 to 500) : show color intialred + (counter*redStep)

My question being, is this the best way to transition between colors, to produce the most aesthetically pleasing colors (without any flickers etc)? Also any thought on the number of transition steps and hold time?

## 3 Answers

It will probably be more aesthetically pleasing to transition between colors using the HSV colorspace.

• Why would that be? Can you explain a bit further? Nov 19 '12 at 16:23
• play with this color wheel: colorschemedesigner.com probably what you think of as transitioning between colors is moving around the color wheel (which is changing HUE). This one is good for you to see saturation: colorpicker.com which will be important if you want to deal with white. Nov 28 '12 at 1:22
• I don't really agree. From my own experimentation, if you are going to pick random colors and then interpolate between them, then using RGB color space is perfectly fine. The one thing you should do however is calculate the euclidean distance between the two color coordinates and use that to calculate the length of time for the color transition. This way you are moving through the RGB color space at a consistent speed. This makes the color transition quite smooth from my testing. If anything, HSL and HSV seemed less so. Oct 8 '14 at 0:01

You use what is called linear interpolation(or some other form).

If A is your start value and B is your stop value then

f(t) = (1 - t)*A + tB


Is a function that has the starting value, t = 0, of A and the stopping value, t = 1, of B.

You can use other functions too, if it fades different, in general,

f(t) = (1 - g(t))*A + g(t)*B


will work for any g(t) as long as g(0) = 0 and g(1) = 1.

so, g(t) = t^n will work and n will control the "speed" or "rate" of fading.

So, in your case, for linear interpolation,

t = counter*Step
Current_Red = (1 - t)*Starting_Red + t*Final_Red


Note when counter = 0 then Current_red = Starting_Red and when counter = final value then CurrentRed = Final_Red.

Obviously when t = 1 then 1 = counter*Step so the final counter value can be computed which will depend on the step size.

You might want to use different interpolation values since we perceive the the components of the colors differently. If you fade them all the same then it might not look as natural.

You probably need to update at least 30 times a second or so. I do not know if the eye will perceive small changes as these over 5 mins. You could probably change the color once a second and no one will know any difference, unless the colors are far apart.

In any case, you have the ability to adjust the parameters quite easily. Try once a second and if you don't like it, increase the speed. Not a big deal.

• so i was thinking along the right lines. interesting point about the eye not perceiving all channels equally. Nov 19 '12 at 17:35

Light transitions depend on the mood of the display and range of effects you can produce.

A stimulating transition might speed up and go thru rapid full saturation or smooth transition color wheel changes like a carnival gambling wheel and stop on the new random colour in a few seconds.

With minimal distraction, a slow transition might interpolate the RGB start and end points and increment the change in 5 seconds time to avoid traffic incidents.

An interesting effect might toggle between next and present colour with slowly changing duty cycle while making a faster transition in the middle of the sweep with a total transition time from 1 to 5 seconds.

Whatever the case, flicker must be avoided at all costs which is more irritating than stimulating (unless you are adolescent) and use a PWM> 500 Hz.

Side comments: added

The most stimulating display I recall was not using LED but using a large TV modified to display quadraphonic music into the 4 colors RGBY and modified the display into a vector scope with amplitude of each color proportional to the log of each amplitude and each color XY vector starting at the center of the display and going radial at 90 deg offsets. The result was seeing what you heard with many loops and colors. But that was in 1970 before LEDs were popular.

I have a XMAS light LED string with 20 transition effects controlled by a single chip micro and a momentary switch to increment your choice of effects, which are wide ranging.

I also have a gif file, I made to simulate smooth transitions of color wheel illumination of exterior lighting of a temple but cannot be inserted here.