@@ -126,8 +126,8 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
126126// prevent new item overwriting an already expired rTarget.
127127const auto now =micros ();
128128const auto itemRemaining = item->callNow .remaining ();
129- const int32_t remaining = rTarget - now;
130- if (!rFirst || (remaining> 0 &&static_cast < decltype ( micros ())>( remaining) > itemRemaining))
129+ const auto remaining = rTarget - now;
130+ if (!rFirst || (remaining<= HALF_MAX_MICROS && remaining > itemRemaining))
131131 {
132132 rTarget = now + itemRemaining;
133133 }
@@ -149,8 +149,9 @@ decltype(micros()) get_scheduled_recurrent_delay_us()
149149{
150150if (!rFirst)return HALF_MAX_MICROS;
151151// handle already expired rTarget.
152- const int32_t remaining = rTarget -micros ();
153- return (remaining >0 ) ?static_cast <decltype (micros ())>(remaining) :0 ;
152+ const auto now =micros ();
153+ const auto remaining = rTarget - now;
154+ return (remaining <= HALF_MAX_MICROS) ? remaining :0 ;
154155}
155156
156157decltype (micros()) get_scheduled_delay_us()
@@ -262,8 +263,8 @@ void run_scheduled_recurrent_functions()
262263// prevent current item overwriting an already expired rTarget.
263264const auto now =micros ();
264265const auto currentRemaining = current->callNow .remaining ();
265- const int32_t remaining = rTarget - now;
266- if (remaining> 0 &&static_cast < decltype ( micros ())>( remaining) > currentRemaining)
266+ const auto remaining = rTarget - now;
267+ if (remaining<= HALF_MAX_MICROS && remaining > currentRemaining)
267268 {
268269 rTarget = now + currentRemaining;
269270 }