Scheduler_example3.ino 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #define _TASK_SLEEP_ON_IDLE_RUN
  2. #include <TaskScheduler.h>
  3. #define LEDPIN 13
  4. Scheduler ts;
  5. Task tWrapper(30000L, -1, &WrapperCallback, &ts, true);
  6. Task tBlink(5000, 1, NULL, &ts, false, &BlinkOnEnable, &BlinkOnDisable);
  7. Task tLED(0, -1, NULL, &ts, false, NULL, &LEDOff);
  8. void WrapperCallback() {
  9. tBlink.restartDelayed(); // LED blinking is initiated
  10. //every 30 seconds for 5 seconds
  11. }
  12. // Upon being enabled, tBlink will define the parameters
  13. // and enable LED blinking task, which actually controls
  14. // the hardware (LED in this example)
  15. bool BlinkOnEnable() {
  16. tLED.setInterval( 500 + random(501) );
  17. tLED.setCallback( &LEDOn);
  18. tLED.enable();
  19. return true; // Task should be enabled
  20. }
  21. // tBlink does not really need a callback function
  22. // since it just waits for 5 seconds for the first
  23. // and only iteration to occur. Once the iteration
  24. // takes place, tBlink is disabled by the Scheduler,
  25. // thus executing its OnDisable method below.
  26. void BlinkOnDisable() {
  27. tLED.disable();
  28. }
  29. void LEDOn () {
  30. digitalWrite(LEDPIN, HIGH);
  31. tLED.setCallback( &LEDOff);
  32. }
  33. void LEDOff () {
  34. digitalWrite(LEDPIN, LOW);
  35. tLED.setCallback( &LEDOn);
  36. }
  37. // Note that LEDOff method serves as OnDisable method
  38. // to make sure the LED is turned off when the tBlink
  39. // task finishes (or disabled ahead of time)
  40. void setup() {
  41. // put your setup code here, to run once:
  42. }
  43. void loop() {
  44. // put your main code here, to run repeatedly:
  45. ts.execute();
  46. }