MultipleBlinks.ino 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Multiple Blinks
  3. Demonstrates the use of the Scheduler library for the Arduino Due
  4. Hardware required :
  5. * LEDs connected to pins 11, 12, and 13
  6. created 8 Oct 2012
  7. by Cristian Maglie
  8. Modified by
  9. Scott Fitzgerald 19 Oct 2012
  10. This example code is in the public domain
  11. http://www.arduino.cc/en/Tutorial/MultipleBlinks
  12. */
  13. // Include Scheduler since we want to manage multiple tasks.
  14. #include <Scheduler.h>
  15. int led1 = 13;
  16. int led2 = 12;
  17. int led3 = 11;
  18. void setup() {
  19. Serial.begin(9600);
  20. // Setup the 3 pins as OUTPUT
  21. pinMode(led1, OUTPUT);
  22. pinMode(led2, OUTPUT);
  23. pinMode(led3, OUTPUT);
  24. // Add "loop2" and "loop3" to scheduling.
  25. // "loop" is always started by default.
  26. Scheduler.startLoop(loop2);
  27. Scheduler.startLoop(loop3);
  28. }
  29. // Task no.1: blink LED with 1 second delay.
  30. void loop() {
  31. digitalWrite(led1, HIGH);
  32. // IMPORTANT:
  33. // When multiple tasks are running 'delay' passes control to
  34. // other tasks while waiting and guarantees they get executed.
  35. delay(1000);
  36. digitalWrite(led1, LOW);
  37. delay(1000);
  38. }
  39. // Task no.2: blink LED with 0.1 second delay.
  40. void loop2() {
  41. digitalWrite(led2, HIGH);
  42. delay(100);
  43. digitalWrite(led2, LOW);
  44. delay(100);
  45. }
  46. // Task no.3: accept commands from Serial port
  47. // '0' turns off LED
  48. // '1' turns on LED
  49. void loop3() {
  50. if (Serial.available()) {
  51. char c = Serial.read();
  52. if (c == '0') {
  53. digitalWrite(led3, LOW);
  54. Serial.println("Led turned off!");
  55. }
  56. if (c == '1') {
  57. digitalWrite(led3, HIGH);
  58. Serial.println("Led turned on!");
  59. }
  60. }
  61. // IMPORTANT:
  62. // We must call 'yield' at a regular basis to pass
  63. // control to other tasks.
  64. yield();
  65. }