contrem / arduino-timer

Non-blocking library for delaying function calls
BSD 3-Clause "New" or "Revised" License
334 stars 50 forks source link

reseting the timer #86

Closed dave5992 closed 2 days ago

dave5992 commented 1 year ago

It would be great if we could get a reset funtion

philj404 commented 1 year ago

I'm not sure what you mean by a 'reset function'.

Does timer.cancel(aTaskId); do what you want?

dave5992 commented 10 months ago

if timer.cancel(); resets the time to 0 then yes.

philj404 commented 10 months ago

By default the timer uses millis() to determine the current time (time since last hard reset). The timer only reads the value of millis() -- never modifies it directly. I don't know if there's a way to reset it to zero without a hard reset.

If millis() starts returning earlier values, the timer will get confused about when a pending task should come due.

timer.cancel() just clears all pending tasks. That's probably good enough to do what you need. The timer tasks don't particularly care what value millis() returns, they just check if millis() has increased beyond the delay requested (relative to when the task was created).

(This is probably not worth the effort, but...) You can substitute your own function (and do your own resetting) instead of using the default millis(). See an example simMillis() in https://github.com/contrem/arduino-timer/blob/master/extras/tests/timerTest/timerTest.ino. I use it to run tests without actually waiting for millis() to elapse/increment over seconds.