Skip to content

delay() in setup - The delay prevents a task from executing. #214

Closed

Description

@hoffmakl1961

In the example: Scheduler_example00_Blink.ino

You've coded two delay(x) in the setup function. That adds up to 3 seconds.
However, the scheduler doesn’t start until **ts.execute()** is called in loop().
By that point, more than 3 seconds have passed, but several 500-ms intervals have already elapsed.
The library attempts to catch up on these missed intervals.
This results in rapid function calls.

To measure the time, I modified the blink1CB() function:

bool LED_state = false;
// === 1 =======================================
void blink1CB() {
  static uint8_t Anzahl = 0;
  if ( tBlink1.isFirstIteration() ) {
    _PP(millis());
    _PL(": Blink1 - simple flag driven");
    LED_state = false;
  }
  Anzahl++;                   // number of call 
  // Serial.printf("Aufruf: %u \tbei %ums\n", Anzahl, millis());
  Serial.printf("Call: %u \tat %ums\n", Anzahl, millis());

  if ( LED_state ) {
    LEDOff();
    LED_state = false;
  }
  else {
    LEDOn();
    LED_state = true;
  }

  if ( tBlink1.isLastIteration() ) {
    tBlink2.restartDelayed( 2 * TASK_SECOND );
    LEDOff();
  }
}

Monitor output:
3027: Blink1 - simple flag driven
Call: 1 at 3027ms
Call: 2 at 3027ms
Call: 3 at 3027ms
Call: 4 at 3027ms
Call: 5 at 3027ms
Call: 6 at 3027ms
Call: 7 at 3038ms

Call: 8 at 3501ms
Call: 9 at 4001ms
Call: 10 at 4501ms
Call: 11 at 5001ms
Call: 12 at 5501ms
Call: 13 at 6001ms
Call: 14 at 6501ms
Call: 15 at 7001ms
Call: 16 at 7501ms
Call: 17 at 8001ms
Call: 18 at 8501ms
Call: 19 at 9001ms
Call: 20 at 9501ms

The first 7 function calls occur within 10 ms; after that, everything works as intended every 500 ms.

Solution: Start the task only after initialization (after the delays).
Conclusion:
Task tBlink1(PERIOD1, DURATION / PERIOD1, &blink1CB, &ts, false);

At the end of Setup
tBlink1.enableDelayed(PERIOD1);

Activity

  1. arkhipenko commented on Oct 21, 2026

    @arkhipenko
    Owner

    confirm. will be fixed in the next release

  2. added this to the v4.1.1 milestone on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions