Fading setColor help!

Is there a way to fade a setColor down to 0? Previously I have been able to fade a single color by doing something like:

int brightness;
int fade=1;

analogWrite(red, brightness);
brightness = brightness - fade;
delay(100);

and if I want it to pulse, I add

if (brightness==0||brightness==255){
fade=-fade;
}

But if I wanted a more specific color, I could do:

void loop() {
setColor(255,50,0);
delay(10);
}

void setColor (int blueV, int redV, int greenV){
analogWrite(red, redV);
analogWrite(blue,blueV);
analogWrite(green,greenV);
}

However, I cant figure out how to make this new specific color fade. I'm not too familiar with the void setColor or adding any other void other than the loop for that matter.

please help!!

A void does not mean anything. You are talking about functions here. This one just sets the color of a RGB led probably. Those leds include 3 small leds one of each R,G,B color and by setting different values to how intense red, green and blue are then you can get plenty of colors shades

But fading Colors is more complicated...imagine you are in a 3D space and coordinates are R, G and B
There are infinite numbers of paths taking you from a given (r,g,b) point to (0,0,0)

For example you could diminish first the red to 0, then the green and then the blue, or blue first may be, or may be green twice as fast as the others or all at the same time...

so the question is which path do you want to take. Usually you would want to maintain the perception of the same « color » but less bright. Given how colors blend and your eyes work, fading the 3 at the same time linearly does not give fantastic results

Color Theory helps somewhat solve this if you shift form RGB space to HSB (hue, saturation, brightness) then you can just dim the brightness to zero while conserving the hue and saturation and that will feel more natural.

There is plenty on line to read about this and libraries implement the transformation between one space to the other

Thanks J-M-L for the reply!

Color Theory helps somewhat solve this if you shift form RGB space to HSB (hue, saturation, brightness) then you can just dim the brightness to zero while conserving the hue and saturation and that will feel more natural.

Using what you said, I was able to rewrite my code into this:

bB=(0.0brightness);
bG=(0.1
brightness);
bR=(0.9*brightness);

brightness=brightness+fade;
if(brightness<=50){
fade=1;
}
if(brightness>=255){
fade=-1;
}
delay(5);

With this, I can use the percentage of the color I want, say 90% Blue and 10% Green and 0% Red in this case, then I can fade the brightness and the color will have the same percentage of each as the brightness goes down.

My new issue is if I wanted to fade smoothly from one color into another. I cant get that to work at all. Any ideas? Like a 90%, 10%, 0% to a 10%, 0%, 90%.

MajesticLamb:
Thanks J-M-L for the reply!
Using what you said, I was able to rewrite my code into this:
With this, I can use the percentage of the color I want, say 90% Blue and 10% Green and 0% Red in this case, then I can fade the brightness and the color will have the same percentage of each as the brightness goes down.

That is exactly what I said does NOT work well because colors do not blend linearly, you are still in RGB space... But given it’s a RGB led and usually they are not great at giving millions of shades anyway, that’s a first approach (but may be you could have read a bit about moving from RGB to HSB... disappointed :frowning: )

Fading to another color (from point (r1, g1 b1) to (r2, g2, b2) ) is exactly the same issue.
This is also best handled in HSB space. This time if would be by just playing on the Hue and keep saturation and brightness the same.

But as a poor approximation you can take the same approach of playing in RGB space and you can have a linear interpolation moving you step by step from r1 to r2, g1 to g2 and b1 to b2. That’s a simple math equation of type y = ax+b. When x is 0 you want to be at y=r1 and say if you want 10 steps to get to the other point then when x is 10 you want y to be r2. That’s a system with two equations and two unknowns (a and b) so easy to solve for in the general case. Then you apply a similar equation to green and blue each with their own affine function.


Please edit and correct your post above and add code tags around your code instead of a quote tag.
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)