Scheduler_example13_Micros.ino 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * TaskScheduler Test of microsecond scheduling resolution
  3. *
  4. * Task 1 runs starting with 211 microseconds intervals, doubling the interval on every iteration
  5. * until it wraps when interval reaches about 72 minutes mark
  6. *
  7. * Task 2 provides heartbeat at a steady 5 seconds intervals
  8. *
  9. */
  10. #define _TASK_MICRO_RES
  11. #include <TaskScheduler.h>
  12. #define T1_INIT (211L)
  13. Scheduler runner;
  14. // Callback methods prototypes
  15. void t1Callback();
  16. void t1OnDisable();
  17. void t2Callback();
  18. unsigned long t1_interval = T1_INIT;
  19. // Tasks
  20. Task t1(t1_interval, 1, &t1Callback, &runner, true, NULL, &t1OnDisable); //adding task to the chain on creation
  21. Task t2(5 * TASK_SECOND, TASK_FOREVER, &t2Callback, &runner, true); //adding task to the chain on creation
  22. void t1Callback() {
  23. unsigned long t = micros();
  24. Serial.print("t1: ");
  25. Serial.println(t);
  26. }
  27. void t1OnDisable() {
  28. t1_interval += t1_interval;
  29. if (t1_interval < T1_INIT) t1_interval = T1_INIT;
  30. t1.setInterval(t1_interval);
  31. t1.restartDelayed();
  32. }
  33. void t2Callback() {
  34. unsigned long t = micros();
  35. Serial.print("t2: ");
  36. Serial.print(t);
  37. Serial.println(" heartbeat");
  38. }
  39. void setup () {
  40. Serial.begin(115200);
  41. Serial.println("Scheduler TEST Microsecond Resolution");
  42. Serial.println("5 seconds delay");
  43. delay(5000);
  44. runner.startNow(); // This creates a new scheduling starting point for all ACTIVE tasks.
  45. // PLEASE NOTE - THIS METHOD DOES NOT ACTIVATE TASKS, JUST RESETS THE START TIME
  46. t1.delay(); // Tasks which need to start delayed, need to be delayed again after startNow();
  47. // Alternatively, tasks should be just enabled at the bottom of setup() method
  48. // runner.enableAll();
  49. // t1.delay();
  50. }
  51. void loop () {
  52. runner.execute();
  53. }