I've currently managed to get my LED to cycle through eight colors that I've selected. Everything is working correctly, except that I want to go for a more natural feel, and would like to fade / transition from one color to the next, instead of having them just replace one another.
Here's my code so far:
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(250, 105, 0); // Yellow
delay(1000);
setColor(250, 40, 0); // Orange
delay(1000);
setColor(255, 0, 0); // Red
delay(1000);
setColor(10, 10, 255); // Blue
delay(1000);
setColor(255, 0, 100); // Pink
delay(1000);
setColor(200, 0, 255); // Purple
delay(1000);
setColor(0, 255, 0); // Green
delay(1000);
setColor(255, 255, 255); // White
delay(1000);
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, 255-red);
analogWrite(greenPin, 255-green);
analogWrite(bluePin, 255-blue);
}
What the other answers omit about this topic is the fact that that human perception of light intensity is logarithmic, not linear. The analogWrite()
routines are setting the output pin's PWM duty cycle, and are linear. So by taking the minimum duty cycle (say 0
) and maximum duty cycle (say, for the sake of easy math this is 10
) and dividing it into equal chunks, you will be controlling the intensitiy linearly which will not give satisfying results.
What you need to do instead is set your intensity exponentially. Let's say your maximum intensity is 255
. You can generate this result by treating your intensity as a power to raise some number to. In our case, given that we are dealing with computers that like binary, powers of two are convenient. So,
2^0 =1
2^8=256
so we can have 8 intensity levels. Actually, note that out minimum is now not fully off (it is 1
not 0
) and our maximum is out of range (256
not 255
). So we modify the formula to be
output = 2 ^ intensity - 1
Or in code
int output = 1<<intensity - 1;
This yields values from 0 to 255 for intensity levels from 0
to 8
(inclusive), so we actually get nine levels of intensity. If you wanted smoother transitions (i.e. more levels of intensity), and still use logarithmic intensity you'll need floating-point math.
If you apply this method of calculating intensity to each channel (R, G, B) then your perception will be in accord with what your code says it should be.
As fars as how to smoothly transition between various colors, the answer depends on how you want to navigate the color space. The simplest thing to do is to think about your color space as a triangle, with R, G, and B, as the verteces:
The question then is how to navigate this triangle: you could go along the sides, from R, to G, to B. This way you will never see white (all channels fully on) or "black" (all fully off). You could think of your color space as a hexagon, with additional purple (R+B), yellow (G+B), and brown (R+G) colors, and also navigate the perimeter (again, no white or black). There are as many fading possibilities as there are ways of navigating insides these, and other figures we might think of.
When I built fading programs like this the color space and the traversal I liked was as follows: think of each channel as a binary bit, so now you have three (R, G, and B). If you think of each color as having some combination of these channels being fully on, you get 7 total colors (excluding black, but including white). Take the first of these colors, fade to it from black and back to black, and then go to the next color. Here's some code that does something like that:
int targetColor = 1;
int nIntensity = 0;
int nDirection = 1; // When direction is 1 we fade towards the color (fade IN)
// when 0 we fade towards black (fade OUT)
#define MAX_INTENSITY 8
#define MIN_INTENSITY 0
#define MAX_TARGETCOLOR 7
void loop() {
for (;;) {
// Update the intensity value
if (nDirection) {
// Direction is positive, fading towards the color
if (++nIntensity >= MAX_INTENSITY) {
// Maximum intensity reached
nIntensity = MAX_INTENSITY; // Just in case
nDirection = 0; // Now going to fade OUT
} // else : nothing to do
} else {
if (--nIntensity <= MIN_INTENSITY) {
nIntensity = MIN_INTENSITY; // Just in case
// When we get back to black, find the next target color
if (++targetColor>MAX_TARGETCOLOR)
targetColor=1; // We'll skip fading in and out of black
nDirection = 1; // Now going to fade IN
} // else: nothing to do
}
// Compute the colors
int colors[3];
for (int i=0;i<3;i++) {
// If the corresponding bit in targetColor is set, it's part of the target color
colors[i] = (targetColor & (1<<i)) ? (1<<nIntensity) -1 : 0;
}
// Set the color
setColor(colors[0], colors[1], colors[2]);
// Wait
delay(100);
}
}