Scheduler_example06_IDLE.ino 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * This is a test to prove that processor really goes into IDLE sleep.
  3. * For this setup:
  4. *
  5. *
  6. Task c(10, -1, &Count, &ts);
  7. Task t(10000, 1, NULL, &ts, true, &tOn, &tOff);
  8. The result are:
  9. 1): With #define _TASK_SLEEP_ON_IDLE_RUN enabled
  10. On Arduino Uno:
  11. Start
  12. c1=10771 - v2.5.0 (v1.9.0: same)
  13. c2=1001
  14. On Teensy 3.5 (120MHz ARM):
  15. Start
  16. c1=21065
  17. c2=1001
  18. and
  19. 2): With #define _TASK_SLEEP_ON_IDLE_RUN disabled (commented out)
  20. Arduino Uno:
  21. Start
  22. c1=635735 - v2.5.0 (v1.9.0: 551947)
  23. c2=1001
  24. On Teensy 3.5 (120MHz ARM):
  25. Start
  26. c1=2690322
  27. c2=1001
  28. C1 is scenario 2) is much higher than in scenario 1) because processor is put to sleep for 1), but not for 2)
  29. */
  30. /**
  31. * Compile and run once with _TASK_SLEEP_ON_IDLE_RUN enabled, then with _TASK_SLEEP_ON_IDLE_RUN disabled.
  32. * Compare the results.
  33. */
  34. //#define _TASK_SLEEP_ON_IDLE_RUN
  35. #include <TaskScheduler.h>
  36. Scheduler ts;
  37. // Callback methods prototypes
  38. void Count();
  39. bool tOn(); void tOff();
  40. // Tasks
  41. Task c(10, TASK_FOREVER, &Count, &ts);
  42. Task t(10000, TASK_ONCE, NULL, &ts, true, &tOn, &tOff);
  43. volatile unsigned long c1, c2;
  44. bool tOn() {
  45. c1 = 0;
  46. c2 = 0;
  47. c.enable();
  48. return true;
  49. }
  50. void tOff() {
  51. c.disable();
  52. Serial.print("c1=");Serial.println(c1);
  53. Serial.print("c2=");Serial.println(c2);
  54. }
  55. void setup() {
  56. // put your setup code here, to run once:
  57. Serial.begin(115200);
  58. Serial.println("Start");
  59. t.delay();
  60. // ts.allowSleep(false);
  61. }
  62. void Count() {
  63. c2++;
  64. }
  65. void loop() {
  66. // put your main code here, to run repeatedly:
  67. ts.execute();
  68. c1++;
  69. }