diff --git a/src/timer.h b/src/timer.h index c441420..4d45538 100644 --- a/src/timer.h +++ b/src/timer.h @@ -92,7 +92,13 @@ class Timer { struct task * const task = &tasks[i]; const unsigned long duration = t - task->start; - if (task->handler && duration >= task->expires) { + // Tasks that were generated by a task earlier in the loop could have + // a start time larger than the "current time" given as an argument. + // + // Check if t >= task->start to prevent the underflow from triggering tasks early. + // + // See https://github.com/contrem/arduino-timer/pull/7 for more info. + if (task->handler && t >= task->start && duration >= task->expires) { task->repeat = task->handler(task->opaque) && task->repeat; if (task->repeat) task->start = t;