arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.22k stars 224 forks source link

Include example for one-off task #100

Closed skorokithakis closed 4 years ago

skorokithakis commented 4 years ago

I have a relay I need to turn on and then off after N seconds. I see the LED example, but it defines multiple tasks, and understanding the flow becomes a bit complicated. Is there an example where a single, one-off task is scheduled for N seconds, with an "on" and an "off" handler? That would be very much appreciated.

arkhipenko commented 4 years ago

Easy. 

Define a task:

    Scheduler ts;
    Task tRelay (relayOnTime * TASK_SECOND, 2, &callback, &ts);
    ...
    void callback () { 
      if ( tRelay.isFirstIteration() ) {  
        turnRelayOn();  
        return;
      }
      turnRelayOff();
    }

Initiate the task with this:

tRelay.enableDelayed( initial delay );

That's it.  Hope this helps. 

skorokithakis commented 4 years ago

Thank you! Also, is there a way to cancel the task that's already scheduled? Sometimes I have to turn the relay off in an emergency and would like the task (or all tasks) to cancel at that point.

arkhipenko commented 4 years ago

tRelay.disable().There is a Wiki, you know, that you need to read :)On Jul 12, 2020 3:37 PM, Stavros Korokithakis notifications@github.com wrote: Thank you! Also, is there a way to cancel the task that's already scheduled? Sometimes I have to turn the relay off in an emergency and would like the task (or all tasks) to cancel at that point.

—You are receiving this because you commented.Reply to this email directly, view it on GitHub, or unsubscribe.

skorokithakis commented 4 years ago

I searched for "cancel" and "stop" and didn't find this, sorry 😟

arkhipenko commented 4 years ago

https://github.com/arkhipenko/TaskScheduler/wiki/API-Task