You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When an AVR MCU sleeps in power-down mode, it's clock stops running. Typically time is being kept using the watchdog timer, or an asynchronous timer2, to allow waking up after a specified time. However, the millis() and micros() counters are not updated during sleep, meaning that the sketch needs to jump through hoops (manually offsetting these counters, or modifying Arduino's global variables) to make various timing work. The former only works with sketch code, not libraries that rely on both counters, and the latter is obviously very ugly and non-portable.
Ideally, there would be an API that allows modifying the millis() and micros() timers by adding (or perhaps also subtracting?) a specified time from both. Most obvious would be an int32_t specifying the number of microseconds. This allows offsetting the time by about 35 minutes in one call. Alternatively, specifying both millis and micros separately would increase the range, but also complicate the code a bit (needing actual division instead of just bitshifting, I think). In either case, I'd say that both counters are always updated at the same time and stay in sync, not being updated individually.
The text was updated successfully, but these errors were encountered:
The solution to the problem is not simple.
It should extend the core Arduino with the watchdog and sleep function(many people might be happy).
Once you have this feature you can easily update micros() and millis().
shall we?
Adding the ability to add/subtract would also make it easier to test for issues that may only occur after days.
I think an addTime() function with a signed integer microseconds value is the best way to go, but additional setMillis and setMicros would also be useful.
Putting sleep/idle in the core is great, but doesn't address the use case of correcting millis frequency offset, to simplify RTC libraries, or using an external RTC to measure the elapsed sleep time when waking up via interrupt from an unknown source, etc.
On some processors, delay() itself could probably use a low power idle without breaking anything, but that doesn't address deep sleep and probaby can't be safely done on many chips.
When an AVR MCU sleeps in power-down mode, it's clock stops running. Typically time is being kept using the watchdog timer, or an asynchronous timer2, to allow waking up after a specified time. However, the
millis()
andmicros()
counters are not updated during sleep, meaning that the sketch needs to jump through hoops (manually offsetting these counters, or modifying Arduino's global variables) to make various timing work. The former only works with sketch code, not libraries that rely on both counters, and the latter is obviously very ugly and non-portable.Ideally, there would be an API that allows modifying the millis() and micros() timers by adding (or perhaps also subtracting?) a specified time from both. Most obvious would be an int32_t specifying the number of microseconds. This allows offsetting the time by about 35 minutes in one call. Alternatively, specifying both millis and micros separately would increase the range, but also complicate the code a bit (needing actual division instead of just bitshifting, I think). In either case, I'd say that both counters are always updated at the same time and stay in sync, not being updated individually.
The text was updated successfully, but these errors were encountered: