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