Random Twinkling LEDs

So, I have a bit of an interesting project I'm tackling. I'm still relatively new to Arduino and picking it up as I go along. I am working on an old cityscape painting that previously had christmas lights installed behind it (to light up the towers, etc). However, instead of the christmas lights I want to replace them with 5mm pirahna LEDs and an Arduino to control them. There are:

19 amber LEDs: These will be on all the time
13 green LEDs: These will be on all the time
10 blue LEDs: Each of these need to go on randomly at different intervals, and I'd like them to "twinkle"

And on top of all that, I'd like to add a simple on/off switch on the back and a thumbwheel to control brightness.

I've got the LEDs and appropriate resistors and the thumbwheel (still looking for the right on/off switch), I just need some help with the actual programming: the Uno has 6 PWM outputs so I was thinking that I'd put 2 blue LEDs on each output (and 1 on it's own), and then all I need to do is figure out a function to randomize when each output is on and how to accomplish the "twinkle effect". Do you think a flicker effect like the many "candle" examples out there would accomplish the same thing? Or would a twinkle be more like a series of bright, quick flashes? Is there a way to randomize the LEDs so that they sometimes coincide (instead of just having one set on at a random interval)? I think the dimmer thumbwheel would just be as simple as setting a brightness variable that takes the pot as an input, and then that variable would control the maximum brightness of the LEDs (and twinkle effect). Could I wire the amber and green ones to a single output and still control the brightness, or would they need to be on a PWM output to accomplish that as well?

Any help or code snippets you could offer would be greatly appreciated. I'll post my progress as I work through it.

This is an example "flicker" that I found here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192693288
Maybe I could modify this to have a longer "off duration"? Thoughts?

/*
 * 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<=4; 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<=4; i++){      // init each led with a random value
    ledState[i] = random(20, 201);
  }
}

void loop(){
  for (int i=0; i<=4; i++){                  // for each led:
    analogWrite(ledPin[i], ledState[i]);     // set the pwm value of that pin determined previously
    randNumber = random(-40, 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) {                 // 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
}

Idea: add some potmeters to the Arduino and use their setting to determine the hardcoded values in your sketch.
potmeter 1: average duration of led on
potmeter 2: average duration of led off

use an array of unsigned long to determine the time for the next action of a LED

pseudocode

unsigned long timeArray[20];

for (int i=0; i<20; i++)
{
if (millis() - timeArray > 0)

  • {*
    _ state = digitalRead(LED*);_
    if (state == LOW) timeArray = millis() + analogRead(potmeter1) * factorON + random(-x,x); // if led was OFF it will be switched on so use potmeter 1
    else timeArray = millis() + analogRead(potmeter2) * factorOFF + random(-y,y);
    _ digitalWrite(LED, !state);
    }
    }*

    get the idea?_
1 Like

Makes sense, but the pulse on / pulse off durations will be fairly consistent (I don't want a light to slooowly turn on and off while others flash super quick), but the delay before it pulses again will be random (so that it comes back quickly sometimes and slower other times). That way they'll all light up in a similar fashion, but at different times.

I played around with the flicker sketch last night, and I think I'm sticking to a simple pulse for the LEDs. They look more like city lights that way (or cell towers or the like).

So it's basically just writing something that:

1)turns on the green and amber lights at the maximum brightness (controlled by the thumbwheel)
and
2)runs a pulse routine for each of the blue lights with a randomized delay on each, never exceeding the maximum brightness (controlled by the thumbwheel).

I don't want a light to slooowly turn on and off while others flash super quick

There are many different stars in the universe :slight_smile:

Haha, true. I actually just found a really great example of what I was hoping to do here - http://arduino.cc/forum/index.php/topic,70947.0.html

The only difference is that I'd need to add the chip to my design (which is do-able). I'm going to try and put that together this weekend, and maybe I'll post a video when I'm done.

10 blue LEDs: Each of these need to go on randomly at different intervals, and I'd like them to "twinkle"

You may be able to get the desired effect by having each LED flash at a fixed frequency but have the frequency somewhat different for each LED.

Don

YES!!! This was EXACTLY what I have been looking for!!! Thank you!

How do you generate random twinkling effect for AC lights? I have an Arduino Uno with pins 4,5,6 connected to a AC triac light driver board...

I read the thread on PWM vs digital but can only get one color channel to flicker using code from this thread

Ref: "analogWrite" vs "digitalWrite" - Programming Questions - Arduino Forum