Instructables

RGB LED transition (Arduino)

Picture of 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
 
Remove these adsRemove these ads by Signing Up

Step 1: The circuit

Picture of 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

Picture of 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;
}
nice project! do you have any videos you could post of the sketch above?
simonfrfr (author)  amandaghassaei1 year ago
Sure, Great Idea!, I actually do, I think I will use my Voice recognition system video for this!
maewert1 year ago
Similar instructable here:
http://www.instructables.com/id/Arduino-Examples-1-Make-An-RGB-Led-Randomly-Flash/?comments=all#CTC1DA7GI0TJY7Y

Also I uploaded code to continuously change from one random color to another here (just change the color pins to your pin numbers):
int red = 11; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 9; //this sets the blue led pin

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup()
{ //this sets the output pins
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
redNow = random(255);
blueNow = random(255);
greenNow = random(255);
redNew = redNow;
blueNew = blueNow;
greenNew = greenNow;
}

#define fade(x,y) if (x>y) x--; else if (x<y) x++;

void loop()
{
analogWrite(blue, blueNow);
analogWrite(red, redNow);
analogWrite(green, greenNow);
redNew = random(255);
blueNew = random(255);
greenNew = random(255);
// fade to new colors
while ((redNow != redNew) ||
  (blueNow != blueNew) ||
  (greenNow != greenNew))
{
  fade(redNow,redNew)
  fade(blueNow,blueNew)
  fade(greenNow,greenNew)
  analogWrite(blue, blueNow);
  analogWrite(red, redNow);
  analogWrite(green, greenNow);
  delay(20);
}
}
simonfrfr (author)  maewert1 year ago
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.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!