SimpleTimer icon indicating copy to clipboard operation
SimpleTimer copied to clipboard

millis() wrap

Open irekzielinski opened this issue 9 years ago • 1 comments

After 49 days of running, millis() wraps and SimpleTimer logic will be broken (will fire at wrong time). Check for this condition is needed.

for example:

        if (current_millis - prev_millis[i] >= delays[i]) {

if: prev_millis[0] == 4,294,967,290 (just 5 mills from wrap over) current_millis == 1 (just after wrap over)

"if" statement above will be satisfied for almost any delays[0] value. this will lead to timer being fired pre-maturely

irekzielinski avatar Nov 30 '16 18:11 irekzielinski

The solution is to use "unsigned long" for all variables in the equation that checks the elapsed time against the wanted expiry time (if (current_millis - prev_millis[i] >= delays[i]) {). So delays must also be unsigned long. See also: http://playground.arduino.cc/Code/TimingRollover.

yunjova avatar Mar 27 '17 08:03 yunjova