Smooth fade between RGB led colours

Hi all,

I am still quite new to arduino so I was hoping to get some help.

I have a code connected to RGB led which switches between 3 colours (yellow, orange and red). However, the transition is not smooth enough and because it is a loop it starts again. How I could have a smoother transition and make it fade out completely when it reaches red?

int redPin= 11;
int greenPin = 10;
int bluePin = 9;

void colourTransition();
void setup()
{
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
}
void loop()
{
  colourTransition();
}
void colourTransition()
{
  
 setColor(235, 160, 0);
 delay(1000);
 setColor(245, 100, 0);
 delay(1000);
 setColor(255, 0, 0);
 delay(1000);
  }
  void setColor(int red, int green, int blue)
  {
    analogWrite(redPin, 255-red);
    analogWrite(greenPin, 255-blue);

  }

What it is? Why this procedure is calling "setColor", although it has nothing to do with color set?

To produce the actual colour. Or how would you do it then?

Why the colors are inverted? - (255-red) instead of just red?
Why the blue color value controls the greenPin?
Where the green color itself?

to fade out.
I don't know why, but when I put blue to control greenPin it eliminates blue colour, which is what I need to produce yellow, orange and red.

To fade LED out you should gradually decrease the brightness of the led, usually in the loop.
I don't see anything like in the code

Hmm, makes sense of course. But the colours in my code sort of fade between each other, it's just not very smooth...that's my problem

So you need more steps between them.
The easiest way to do it - add the loop, that will slowly increment or decrement color value from starting color to the desired one.

Alright, I'll try, thank you!

Perhaps have a look at some of the libraries that deal with these things. E.g.,

You may be particularly interested in the crossFade and fadeOut functions.

and

Welcome and thanks for using code tags in your first post.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Thank you so much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.