Arduino DISCUSSION

Will a millis-based Arduino task still run after the counter wraps?

Started by hana Arduino millisunsigned arithmetictimer rollovernonblocking codescheduling
3 replies 248 views 4 participants
Latest activity · 27 Sep 2026

Will a millis-based Arduino task still run after the counter wraps?

hana Arduino Forum
#1

Community replies 3

Re: Will a millis-based Arduino task still run after the counter wraps?

#2

Store timestamps and intervals in the unsigned type appropriate to the counter. For the usual unsigned-long millis API, compare elapsed time using (unsigned long)(now - previous) >= interval. Unsigned subtraction handles the wrap modulo the counter range. This assumes elapsed intervals are shorter than a full wrap and the task is checked often enough.

Re: Will a millis-based Arduino task still run after the counter wraps?

#3

Make the timing logic accept a supplied timestamp during a test. Feed it values just before and just after the unsigned maximum, then check that the task becomes due exactly when intended. Include a delayed iteration as well. There is no need to modify the core's real timer to test the arithmetic.

Re: Will a millis-based Arduino task still run after the counter wraps?

#4

Choose whether the next schedule uses the current time or advances the previous deadline by the period. The former avoids catch-up bursts but can drift; the latter can preserve cadence but needs an explicit missed-period policy. Keep long-duration calendar time separate from this short-interval scheduler, and avoid reconfiguring the core's timing source accidentally.

TEP COMMUNITY