Random Flash and Flicker Without Delay

Hello all! I've come a long way with my project and have learned how to multitask and have a plan for how to implement the many LED, sound, and servo components that I need for this project. I am, however, still struggling with a couple of things.

First, I've been trying to get a realistic lightning effect from a string of LEDs. I've tried various codes for random flashes, and even tried hooking the output of the sound shield I'm using to an input pin to watch for certain levels and turn on and off the strip accordingly, but nothing seems to work reliably. To make it even more complicated, since I'm running a lot of things at "once" I don't want to use delay. Thus, I'm just curious if anyone had any suggestions as to what to do. I would need the effect to turn on at a certain time and turn off at a certain time, as well. (It's not going for the whole program). So, I almost need a loop within a loop of some kind.

My second issue is sort of the same. I want a strip of LEDs to appear to be flickering like candles. Like the lightning strip, the effect needs to be able to be on until a certain time and off for a bit, then back on. My main issue here is implementing this effect without delay using millis and not screwing up the other components of the program.

Attached is what I've got with the project thus far. I've experimented with the lightning, and have been able to dictate when things turn on and off using timers, but the specific code for the above desired effects is what I've really been struggling with.

Any help would be very much appreciated. Thanks!

HMSR2.ino (5.88 KB)

Please read the forum guidelines in the sticky post. It will tell you what you need to include in your post when you ask for help, how to post code, links etc correctly. Then edit your post above and fix please.

So, I almost need a loop within a loop of some kind.

No, that would have a similar effect to using delay(). It would prevent your code from doing the other things it needs to do. Very short loops, that last only a fraction of a second, are ok and normal. But loops which last for longer periods are not. What you need to do instead is use millis() like a stopwatch to time when to start and when to stop an activity. Then you can use if() to perform the action if the stopwatch is in the required time range. Unlike a stopwatch, millis() "ticks" 1000 times per second, but that's no problem for a chip with a clock speed of 16 million ticks per second.

For candle flicker effects, you may be better using a pwm pin. Using millis() again, you can update the pwm level every 100ms for example. Each time you update the pwm level, increase or decrease it by a random amount (but make sure it stays between 0 and 255).