arduino-timer icon indicating copy to clipboard operation
arduino-timer copied to clipboard

reseting the timer

Open dave5992 opened this issue 2 years ago • 3 comments
trafficstars

It would be great if we could get a reset funtion

dave5992 avatar Aug 28 '23 18:08 dave5992

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

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

philj404 avatar Sep 01 '23 23:09 philj404

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

dave5992 avatar Dec 09 '23 14:12 dave5992

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.

philj404 avatar Dec 09 '23 18:12 philj404