Wednesday, March 25, 2009

Flickering LEDs - proof of concept

My first project is to light a string of papier-mâché hot air balloons. I was just going to light them with some LEDs, but then I heard about the Arduino and wanted to make something more exciting. I got a starter kit from adafruit.com and got to work.

This is the first proof of concept of making the LEDs flicker. Later I’ll add a light sensor to start a sleep timer that will turn the balloons off after a half hour.



Here’s the sketch that runs what you see above:

/*
* randomly flickering LEDs
*/

int ledPin[] = {
5, 6, 9, 10, 11}; // pwm pins only
int ledState[5]; // last state of each led
long randNumber;

void setup() {
for (int i=0; i<5; i++){ // set each led pin as an output
pinMode(ledPin[i], OUTPUT);
}
randomSeed(analogRead(0)); // seed the rnd generator with noise from unused pin

for (int i=0; i<5; i++){ // init each led with a random value
ledState[i] = random(20, 201);
}
}

void loop(){
for (int i=0; i<5; i++){ // for each led:
analogWrite(ledPin[i], ledState[i]); // set the pwm value of that pin determined previously
randNumber = random(-35, 41); // generate new random number and add that to the current value
ledState[i] += randNumber; // that range can be tweaked to change the intensity of the flickering
if (ledState[i] > 200) { // now clamp the limits of the pwm values so it remains within
ledState[i] = 200; // a pleasing range as well as the pwm range
}
if (ledState[i] < 10) {
ledState[i] = 10;
}
}
delay(100); // the delay between changes
}

No comments:

Post a Comment