RGB LED Transition (Arduino)

41,290

49

14

Posted

Introduction: RGB LED Transition (Arduino)

Hello Guys, in this instructable I will give you a code for transtioning an LED. My group, Newton Labs, has spent quite a bit of time on this. Transitioning isn't as easy as it seems for a nice smooth transition.






Submitted by Newton Labs for the Instructables Sponsorship Program

Step 1: The Circuit

In this circuit, you will need to use 3 Pulse Width Modulation pins, PWM for short. Pins that are not specified as PWM pins are digital pins, which can only turn on and off. PWM pins can go from lets say 3.3 volts to five volts to 1.1 volts.

In my program  Red = pin 3, Blue = pin 5, and Green = pin 6.

Step 2: The Code

int prevR = 0, prevG = 0, prevB = 0; // all of the previous RGB values
int const Red = 3; //pin 3 
int const Blue = 5; // pin 4
int const Green = 6; // pin 5

void setup(){} // sets up the program
void loop() { //main loop of the program
  RGB(255, 255, 255); // this calls the RGB function
  delay(1000); //stays on white for one second
  RGB(0, 0, 255); 
   delay(1000);
   RGB(0,120,255);
  delay(1000);
    RGB(0, 255, 0);
  delay(1000);
    RGB(255, 0, 255);
  delay(10);
    RGB(0,0,0);
  delay(1000);
 
}


void RGB(int R, int G, int B) {
for (int i = 0; i <= 255; i++)
{
if (i >=  prevR - R && prevR < R) {
 
  analogWrite(Red, prevR + i);
}
if (i >= prevG - G && prevG < G) {
 
  analogWrite(Green, prevG + i);
 
}
if (i >= prevB - B && prevB < B) {
 
  analogWrite(Blue, prevB + i);
 
}
//delay(10);
//}
//for (int i = 0; i <= 255; i++)
//{
if (i >= R - prevR && prevR > R) {
 
  analogWrite(Red, prevR - i);
}
if (i >= G - prevG && prevG > G) {
 
  analogWrite(Green, prevG - i);
 
}
if (i >= B - prevB && prevB > B) {
 
  analogWrite(Blue, prevB - i);
 
}
delay(10);
}
delay(10);
analogWrite(Red, R);
analogWrite(Green, G);
analogWrite(Blue, B);
prevR = R;
prevG = G;
prevB = B;
}

Step 3: Hooray!

You are done and you should have Revision 3 of my RGB LED transitioning program! 

Share

  • Nice job! One note. ...-redkcir

    redkcir made it!

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • Gluten Free Challenge

    Gluten Free Challenge
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

13 Comments

i've been trying to make my own but i've had no luck,your one is awesome!

I've programmed two other ways to transition this, and working on a 3rd that takes into account how LED's are observed by the human eye, but for the time being for a simpler to understand program (as I feel programming should be just as much about getting the job done as it should be about clairty) you can try looking through this.

This is the simplest one I have so far.

int const Red=3, /* Pin 3 */ Blue=5, /* Pin 5 */ Green=6; /* Pin 6 */
void RGB(int R,int G,int B){

analogWrite(Red,R);
analogWrite(Green,G);
analogWrite(Blue,B);
delay(25);
}
void setup(){}
void loop(){

//RED to YELLOW
int r =255, g =0, b =0;

for(int i =0; i <=255;++i){

RGB(r,i,b);

}

//YELLOW to GREEN

r =255; g =255;b =0;

for(int i =255; i >=0;--i){

RGB(i,g,b);

}

//GREEN to TEAL


r =0;g =255;b =0;

for(int i =0; i <=255;++i){

RGB(r,g,i);

}
//TEAL to BLUE

r =0;g =255;b =255;

for(int i =255; i >=0;--i){

RGB(r,i,b);

}

//BLUE to MAGENTA

r =0;g =0;b =255;

for(int i =0; i <=255;++i){

RGB(i,g,b);

}

//MAGENTA to RED


r =255;g =0;b =255;

for(int i =255; i >=0;--i){

RGB(r,g,i);

}

delay(1000);

}

how did you make this awesome code,man!?

I've had relatively good luck dimensioning rgb into arrays, then using an array I use a for loop to increment the values based on the next array value and splitting the difference between then and dividing it by 16 where I have 15 analogWrite instances each adding or decreasing 1/16 rgb value . (In my arrays each jump is 32 rgb values +/-) I need to amend the code to deal with the values above 255, but where the values are below 255 the transition is as smooth as butter. Oh and the delay is 100ms between each.

This was a very useful tutorial and program to help me get started in using the Arduino, thank you!


Because of the way the "i" counter works, this program will fail if you start using RGB values that are different than 255 or 0. I had many problems with the software counting too far.

I modified the software and created two completely different ways of processing the data (with slightly different results). If you or anyone is interested, get in touch with me :-) it allows you to custom set the RGB values without affecting the quality of the transition from one colour to the other.

My led is 1 Positive and 3 Negative

The LEDs vary; both variants are sold.

nice project! do you have any videos you could post of the sketch above?