Flickering diorama LED street lamp post

Hello there! Probably this topic has been discussed in past posts but, anyway, here we go.
I am about to finish one of my small dioramas. This one is inspired by the American 'The X-Files' TV show and it is controlled in large part by 3.3V Arduino Gemma type boards (Attiny85). As a final touch, I've added an LED street lamp post (see top-right of my picture) whose light should flicker when the UFO hovers over the street. I know sometimes flickering leds are mostly undesirable but I want to recreate the flickering to add a chilling effect just like those old faulty fluorescent tubes in a lunatic asylum on a stormy night. With that in mind, I made a search on the Internet to find that most of the flickering led effects are candle flame kind of thing. Finally, I found and modified a simulation code by Nikolaj Rahbek (thank you, man!) that handles pretty decently the 'unexplainable phenomenon' of the flickering lamp but I wonder if any of you have already worked on something similar. If that's the case, I will be glad to hear about it. Thank you.
-p

Below, flickering code (code for rotation of UFO with servo not included):

boolean lightOn = 0;

void setup() { 
  pinMode(LED_BUILTIN, OUTPUT);    
}

const int blinkOn[] = {2000, 10, 20, 20, 240, 20, 40, 20, 100, 20, 20, 260, 80, 20, 240, 60, 160, 20, 240, 20, 500, 20, 20, 40, 100, 20, 1000, 340, 660, 20, 800, 5, 60, 5};

void allOn(boolean on) {
  digitalWrite(LED_BUILTIN, on);
}  

boolean myDelay(unsigned int ms) {
  unsigned long startTime = millis();
  unsigned long endTime = startTime+ms;  

  while(millis() < endTime) {
    allOn(lightOn);
    delay(1);
  }
  return 0;
}

void loop() {
  allOn(false);
  delay(2000);  
  for(int i=0; i<sizeof(blinkOn)/sizeof(int); ++i) {
    lightOn = !(i&1);
    if(myDelay(blinkOn[i])) {  
      allOn(false);
      lightOn = false;
      return;
    }
  }
}

Here is a good example of making LEDs flickering by: Jack Christensen.

Thank you LarryD for the link. That looks terrific! -p

I have a non-linear mapper library. You add points X,Y to it and it will put them all together in a curve. Then, you can ask for any X and it will figure out an approximate Y. One of the more popular uses for this is to use X as time in ms and Y as PWM setting. This gives a programmable LED flicker.

-jim lee

Hello
The flickering is a random process.
To achieve this for an LED, the Sketch uses the random() function. The lighting duration and brightness are each calculated with this function. You can play around with the function parameters of the random() functions until the desired appearance for the diorama is achieved.

#define ProjectName "Flickering diorama LED street lamp post"
void setup() {
  //==== Arduino
  Serial.begin(9600);
  Serial.print("File   : "), Serial.println(__FILE__);
  Serial.print("Date   : "), Serial.println(__DATE__);
  Serial.print("Project: "), Serial.println(ProjectName);
  //====
  // put your setup code here, to run once:
  pinMode(5,OUTPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  static unsigned long flickerMillis=0;
  static unsigned long flickerDuration=0;
  if (millis() - flickerMillis >= flickerDuration) {
    flickerMillis=millis();
    flickerDuration=random(50,500);   // flicker duration
    analogWrite(5,  random(0,255));  // flicker brightness
  }
}

Thanks, Larry, Jim, Paul. I know that the chilling effect I am after of is subjective but I will be revising your suggestions to see which one can suit better my diorama. Thank you Arduinoers!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.