millis() software timeout vs Watchdog Timer for graceful cancellation on AVR Arduino
0 reputation · 29 Mar 2020, 18:11 UTC
A design needs a timeout and cancellation path for operations that may block, while keeping the sketch responsive and preserving RAM state across the timeout.
Arduino core offers two documented mechanisms. A non-blocking millis() timer keeps the loop running and allows manual elapsed-time checks with rollover-safe unsigned arithmetic. The hardware Watchdog Timer provides an automatic reset after a configurable period without polling, but a reset clears RAM and loses intermediate state. Core libraries such as Serial and Wire expose timeout arguments and setTimeout methods, yet there is no unified cancellation token in the core APIs. Aborting an in-progress operation therefore depends on library-specific behavior and on custom flag checks.
The unresolved decision is which approach satisfies a concrete constraint of graceful abort without full reset, given the lack of a common cancellation interface.
Does a millis()-based software timeout provide a documented way to abort a blocking library call without leaving peripherals in an undefined state? Can the Watchdog Timer be used as a safety net while still allowing cleanup code to run before reset? Is there a documented pattern for cancelling an ongoing Wire or Serial transaction without relying on vendor-specific functions?