Blink without delay - but with random on and off intervals

Hi Guys I've been trying for several days now to come up with some code that will randomly blink two leds (separately) without using delay. I prefer to use the word flash than blink, as I need each led to have a different on interval to its off interval and the intervals need to be different each time. The final result I'm after is to be able to simulate a couple faulty fluorescent tubes - you know the effect, that annoying unpredictable period flashing that occurs when the starter goes on them! I can't use delays because this code segment needs to be added to an existing sketch, which is already controlling sets of leds (with millis) and needs to be unaffected by this addition.

I do know how to blink an led without delay, [u]but[/u] only if the intervals are fixed, its just how to change the length of each interval between every flash that eludes me. If I define a random interval in setup it uses the one value it calculates for each and every flash, but if I define the interval inside the loop it continually changes before the leds have a chance to flash and so never do flash. I can't see a way of changing the intervals only after the led has flashed, every time. I don't have any code to post yet because this is just an idea at the moment.

Can anyone suggest a method of achieving the effect I'm after? I hope all that makes sense!


Or we could help you with your code. Which you didn't post.

As I mention in the third paragraph - I don't actually have any code yet as I want to work out a method of changing the interval before starting to code. I can write a simple blink without delay sketch, but that won't help me.

I don't mind if it is necessary, but can I ask why the post was moved to projects? Its a coding issue I have.

but only if the intervals are fixed,

So you replaced that fixed time interval with a random number produced by the random() function. You do this after every on and every off action of the LED.

Define an unsigned long variable to hold the duration of the current output state.

Use the blink without delay example to execute some code when that duration has elapsed. The code will toggle the output state, and generate a new random duration.

That's where my tiny brain starts to lose the plot Grumpy_Mike, I start to lose track of what's happening and when. I started to think that maybe flagging when the led was turned on and as soon as that occurred, changing the interval value, but it got awfully confused. Is that the right train of thought though do you think?

OK your reply just came through as well PeterH. Right, so it does seem as though I might be thinking along the right sort of lines, I just need to tackle it again with a fresh piece of paper! I'll sleep on it tonight and try again tomorrow.

Thanks Guys!