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!
<p>i've been trying to make my own but i've had no luck,your one is awesome!</p>
<p>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.<br></p><p>This is the simplest one I have so far.<br><br>int const Red=3, /* Pin 3 */ Blue=5, /* Pin 5 */ Green=6; /* Pin 6 */<br>void RGB(int R,int G,int B){<br><br>analogWrite(Red,R); <br>analogWrite(Green,G); <br>analogWrite(Blue,B);<br> delay(25);<br>}<br>void setup(){}<br>void loop(){</p><p>//RED to YELLOW<br>int r =255, g =0, b =0;</p><p>for(int i =0; i &lt;=255;++i){</p><p>RGB(r,i,b);</p><p>} </p><p>//YELLOW to GREEN</p><p>r =255; g =255;b =0;</p><p>for(int i =255; i &gt;=0;--i){</p><p>RGB(i,g,b);</p><p>} </p><p>//GREEN to TEAL</p><p><br>r =0;g =255;b =0;</p><p>for(int i =0; i &lt;=255;++i){</p><p>RGB(r,g,i);</p><p>} <br>//TEAL to BLUE</p><p>r =0;g =255;b =255;</p><p>for(int i =255; i &gt;=0;--i){</p><p>RGB(r,i,b);</p><p>} </p><p>//BLUE to MAGENTA</p><p>r =0;g =0;b =255;</p><p>for(int i =0; i &lt;=255;++i){</p><p>RGB(i,g,b);</p><p>}</p><p>//MAGENTA to RED</p><p><br>r =255;g =0;b =255;</p><p>for(int i =255; i &gt;=0;--i){</p><p>RGB(r,g,i);</p><p>} </p><p>delay(1000);</p><p>}</p>
<p>how did you make this awesome code,man!?</p>
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!<br><p><br>Because of the way the &quot;i&quot; 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.<br><br>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.</p>
<p>You may try using <a href="http://yaab-arduino.blogspot.com/p/ala.html" rel="nofollow">ALA library</a>. <a href="http://yaab-arduino.blogspot.com/p/ala.html" rel="nofollow">This example</a> shows how easy it is to create color animations with an RGB LED.</p><p>You can also create <a href="http://yaab-arduino.blogspot.com/p/ala-example-rgbledseq.html" rel="nofollow">animation sequences</a>.</p>
<p>My led is 1 Positive and 3 Negative </p>
<p>The LEDs vary; both variants are sold.</p>
<p>Nice job! One note. Your drawing of the leads to the Arduino show that the long lead on the LED is hooked to GND (six pins down from the power plug). This should be to the 5volt plug (5 pins down). I tried to made a video of the project running and have posted it here. This is my first video with a new camera so don't expect a lot. The camera is mounted on a roll of duct tape, a coffee cup (newly emptied) and an old container of Crystal Light Peach Tea (ya do what works, right?). I covered the LED with a piece of paper to tone down the light so the changing of the colors could be seen. I don't know why it is so jumpy, it played fine before I uploaded it.</p>
nice project! do you have any videos you could post of the sketch above?
Sure, Great Idea!, I actually do, I think I will use my Voice recognition system video for this!
Similar instructable here:<br> https://www.instructables.com/id/Arduino-Examples-1-Make-An-RGB-Led-Randomly-Flash/?comments=all#CTC1DA7GI0TJY7Y<br> <br> Also I uploaded code to continuously change from one random color to another here (just change the color pins to your pin numbers):<br> int red = 11; //this sets the red led pin<br> int green = 10; //this sets the green led pin<br> int blue = 9; //this sets the blue led pin<br> <br> int redNow;<br> int blueNow;<br> int greenNow;<br> int redNew;<br> int blueNew;<br> int greenNew;<br> <br> void setup()<br> { //this sets the output pins<br> pinMode(red, OUTPUT);<br> pinMode(green, OUTPUT);<br> pinMode(blue, OUTPUT);<br> redNow = random(255);<br> blueNow = random(255);<br> greenNow = random(255);<br> redNew = redNow;<br> blueNew = blueNow;<br> greenNew = greenNow;<br> }<br> <br> #define fade(x,y) if (x&gt;y) x--; else if (x&lt;y) x++;<br> <br> void loop()<br> {<br> analogWrite(blue, blueNow);<br> analogWrite(red, redNow);<br> analogWrite(green, greenNow);<br> redNew = random(255);<br> blueNew = random(255);<br> greenNew = random(255);<br> // fade to new colors<br> while ((redNow != redNew) ||<br> &nbsp; (blueNow != blueNew) ||<br> &nbsp; (greenNow != greenNew))<br> {<br> &nbsp; fade(redNow,redNew)<br> &nbsp; fade(blueNow,blueNew)<br> &nbsp; fade(greenNow,greenNew)<br> &nbsp; analogWrite(blue, blueNow);<br> &nbsp; analogWrite(red, redNow);<br> &nbsp; analogWrite(green, greenNow);<br> &nbsp; delay(20);<br> }<br> }
Well, I know that my group was the only one who was trying to do this, but its nice to know that we aren't the only ones trying to do this.