Scheduler_example2.ino 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #define _TASK_SLEEP_ON_IDLE_RUN
  2. #include <TaskScheduler.h>
  3. Scheduler runner;
  4. Task t4();
  5. Task t1(2000, 10, &t1Callback, &runner, true); //adding task to the chain on creation
  6. Task t2(3000, -1, &t2Callback, &runner, true); //adding task to the chain on creation
  7. Task t3(5000, -1, &t3Callback);
  8. // Test
  9. // Initially only tasks 1 and 2 are enabled
  10. // Task1 runs every 2 seconds 10 times and then stops
  11. // Task2 runs every 3 seconds indefinitely
  12. // Task1 enables Task3 at its first run
  13. // Task3 run every 5 seconds
  14. // loop() runs every 1 second (a default scheduler delay, if no shorter tasks' interval is detected)
  15. // Task1 disables Task3 on its last iteration and changed Task2 to run every 1/2 seconds
  16. // Because Task2 interval is shorter than Scheduler default tick, loop() executes ecery 1/2 seconds now
  17. // At the end Task2 is the only task running every 1/2 seconds
  18. void t1Callback() {
  19. Serial.print("t1: ");
  20. Serial.println(millis());
  21. if (t1.isFirstIteration()) {
  22. t3.enable();
  23. runner.addTask(t3);
  24. Serial.println("t1: enabled t3 and added to the chain");
  25. }
  26. if (t1.isLastIteration()) {
  27. t3.disable();
  28. runner.deleteTask(t3);
  29. t2.setInterval(500);
  30. Serial.println("t1: disable t3 and delete it from the chain. t2 interval set to 500");
  31. }
  32. }
  33. void t2Callback() {
  34. Serial.print("t2: ");
  35. Serial.println(millis());
  36. }
  37. void t3Callback() {
  38. Serial.print("t3: ");
  39. Serial.println(millis());
  40. }
  41. void setup () {
  42. Serial.begin(115200);
  43. Serial.println("Scheduler TEST");
  44. delay(5000);
  45. }
  46. void loop () {
  47. runner.execute();
  48. Serial.println("Loop ticks at: ");
  49. Serial.println(millis());
  50. }