Scheduler_example6_IDLE.ino 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Start
  11. c1=10771
  12. c2=1001
  13. and
  14. 2): With #define _TASK_SLEEP_ON_IDLE_RUN disabled (commented out)
  15. Start
  16. c1=529783
  17. c2=1001
  18. C1 is scenario 2) is much higher than in scenario 1) because processor is put to sleep for 1), but not for 2)
  19. */
  20. /**
  21. * Compile and run once with _TASK_SLEEP_ON_IDLE_RUN enabled, then with _TASK_SLEEP_ON_IDLE_RUN disabled.
  22. * Compare the results.
  23. */
  24. //#define _TASK_SLEEP_ON_IDLE_RUN
  25. #include <TaskScheduler.h>
  26. Scheduler ts;
  27. // Callback methods prototypes
  28. void Count();
  29. bool tOn(); void tOff();
  30. // Tasks
  31. Task c(10, TASK_FOREVER, &Count, &ts);
  32. Task t(10000, TASK_ONCE, NULL, &ts, true, &tOn, &tOff);
  33. volatile unsigned long c1, c2;
  34. bool tOn() {
  35. c1 = 0;
  36. c2 = 0;
  37. c.enable();
  38. return true;
  39. }
  40. void tOff() {
  41. c.disable();
  42. Serial.print("c1=");Serial.println(c1);
  43. Serial.print("c2=");Serial.println(c2);
  44. }
  45. void setup() {
  46. // put your setup code here, to run once:
  47. Serial.begin(115200);
  48. Serial.println("Start");
  49. t.delay();
  50. // ts.allowSleep(false);
  51. }
  52. void Count() {
  53. c2++;
  54. }
  55. void loop() {
  56. // put your main code here, to run repeatedly:
  57. ts.execute();
  58. c1++;
  59. }