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;
}
}
}