Go Down

### Topic: Studio Bricolage pwm led fading (Read 7754 times)previous topic - next topic

#### graybeard ##### Dec 29, 2008, 03:28 pm
I've just found the Studio Bricolage site (www.studiobricolage.org). They have some excellent ideas and gave an Arduino workshop for artists last summer. Their project included code for fading tricolor leds and color mixing. Here is the code.
//
// random tri-color fading LED *common cathode*
//

int value;
int w;
int x;
int y;
int gradation = 255;
float light_value;
long value_RED;
long value_GREEN;
long value_BLUE;
int ledpin1 = 9;                           // red LED connected to pwm pin 9 via 1K ohm resistor
int ledpin2 = 10;                          // blue LED connected to pwm pin 10 via 1K ohm resistor
int ledpin3 = 11;                          // green LED connected to pwm pin 11 via 1K ohm resistor
// LED cathode connected to ground
void setup()

{
// nothing for setup
}

void loop()
{
value_RED = random(256);                  // puts a random number between 0 and 255 into value_RED
value_BLUE = random(256);                 // puts a random number between 0 and 255 into value_BLUE
value_GREEN = random(256);                // puts a random number between 0 and 255 into value_GREEN

for(value = 0; value <=255; value+=5)                             // ramp the number in "value" from 0 to 255
{
light_value = value_GREEN*((float)value/(float)gradation);
w = light_value;                                                // transfer light_value into an integer
light_value = value_BLUE*((float)value/(float)gradation);
x = light_value;                                                // transfer light_value into an integer
light_value = value_RED*((float)value/(float)gradation);
y = light_value;                                                // transfer light_value into an integer
analogWrite(ledpin1, y);                                        // increase the voltage in pin 9 from 0 to random volts
analogWrite(ledpin2, x);                                        // increase the voltage in pin 10 from 0 to random volts
analogWrite(ledpin3, w);                                        // increase the voltage in pin 11 from 0 to random volts
delay(30);                                                      // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5)                             // ramp the number in "value" from 255 to 0
{
light_value = value_GREEN*((float)value/(float)gradation);
w = light_value;
light_value = value_BLUE*((float)value/(float)gradation);
x = light_value;
light_value = value_RED*((float)value/(float)gradation);
y = light_value;
analogWrite(ledpin1, y);                                        // increase the voltage in pin 9 from random to 0 volts
analogWrite(ledpin2, x);                                        // increase the voltage in pin 10 from random to 0 volts
analogWrite(ledpin3, w);                                        // increase the voltage in pin 11 from random to 0 volts
delay(30);                                                      // waits for 30 milli seconds to see the dimming effect
}
delay(700);                                                        // wait .7 seconds between fade up and down
}

Graybeard

Growing older is mandatory. Growing up is optional

#### anachrocomputer #1
##### Dec 31, 2008, 03:20 am
I just got this code running on my Arduino Duemilanove.  Photo on Flickr: http://www.flickr.com/photos/anachrocomputer/3152525788/

#### seattleandrew #2
##### Jan 23, 2011, 09:35 am
Thanks Graybeard for the code It looks really cool for my project. Any tips for what I should watch out for when I switch to my Common-Anode LED?

Go Up