Trying to understand using millis() for LED blinking

I want an LED to blink at different rates depending on the time before an Alarm.alarmrepeat() in the TimeAlarm library. I'm currently using alarm.delay() for the on/off cycles but, of course, I realize now that doesn't work since the sketch takes time to execute after the 'off' command before it gets back to the 'on' statement so it looks like the 'off' cycle is longer than it should be. There are a couple of recent posts that are telling me (I think) that I should be using millis() instead of delay(). But I don't understand how that can work either. Say I set 'off' at millis() = current and I want to turn it on at current + 250. What happens when the sketch takes longer than 250 before it comes back to the 'on' statement? It seems to me it missed current + 250 and therefore won't work like I expect.

I can't use interrupts to do it since I've already used my #2 and 3 interrupt pins.

What am I missing?

Thanks,

Doug

Say I set 'off' at millis() = current and I want to turn it on at current + 250. What happens when the sketch takes longer than 250 before it comes back to the 'on' statement? It seems to me it missed current + 250 and therefore won't work like I expect.

Your analysis is correct. That's why you should not try to turn turn it on at 'current + 250', you should turn it on at a value that is equal to or greater than 'current + 250'.

I can't use interrupts to do it since I've already used my #2 and 3 interrupt pins.

There are a lot more interrupts than the ones invoked by 'Attach Interrupt'. I'm not sure how many of them are implemented in the Arduino functions, but they are still available. The timer interrupts should be particularly useful in your case.

Don

Don,

Thanks for the response.

If I use the greater than or equal, I won't get the flash rate I want. Is there another way to get a precise flash rate?

You mentioned interrupts - I don't see on my UNO that I have more than 2?

thanks,

Doug

@DDB
I think Arduino only implements the external interrupts, in the UNO there are only two of them. These interrupts are of little use for timing unless you rig up external hardware.

However the chip the Arduino is based on actually has 26 interrupts of which probably 4 are useful for timing.

What happens when the sketch takes longer than 250 before it comes back to the 'on' statement?

Then you will miss the time. It's up to you to ensure that doesn't happen. AFIAK the only way to ensure something happens like this is to use interrupts from a timer or make sure your other code doesn't take too long.

I don't think this sort of "multi-tasking" is well supported on Arduino. Probably because you need a spare timer to do it and they are all used for other functions. It may be that say if you aren't using PWM you can use one of the timers. I'm not sure though which timers are used for what.

Do you need very accurate times? If you are happy with multiples of ~16mS you can use the watch dog timer.


Rob