|
| 1 | +/* |
| 2 | +* Example 02: Ex_02_MultiBlink.ino |
| 3 | +* By: D. Aaron Wisner |
| 4 | +* |
| 5 | +* In this example we are creating a "BlinkProcess" Class that will toggle a pin at a constant period |
| 6 | +* We create 3 BlinkProcess objects that will blink at a diff. period and add them to the scheduler: |
| 7 | +* - blink250 (pin 13) |
| 8 | +* - blink500 (pin 12) |
| 9 | +* - blink1000 (pin 11) |
| 10 | +* Connect an LED to each of these pins and watch them blink |
| 11 | +*/ |
| 12 | + |
| 13 | +#include <ProcessScheduler.h> |
| 14 | + |
| 15 | +// Create my custom Blink Process |
| 16 | +class BlinkProcess : public Process |
| 17 | +{ |
| 18 | +public: |
| 19 | + // Call the Process constructor |
| 20 | + BlinkProcess(Scheduler &manager, ProcPriority pr, unsigned int period, int pin) |
| 21 | + : Process(manager, pr, period) |
| 22 | + { |
| 23 | + _pinState = LOW; // Set the default state |
| 24 | + _pin = pin; // Store the pin number |
| 25 | + } |
| 26 | + |
| 27 | +protected: |
| 28 | + virtual void setup() |
| 29 | + { |
| 30 | + pinMode(_pin, OUTPUT); |
| 31 | + _pinState = LOW; |
| 32 | + digitalWrite(_pin, _pinState); |
| 33 | + } |
| 34 | + |
| 35 | + // Undo setup() |
| 36 | + virtual void cleanup() |
| 37 | + { |
| 38 | + pinMode(_pin, INPUT); |
| 39 | + _pinState = LOW; |
| 40 | + } |
| 41 | + |
| 42 | + // Create our service routine |
| 43 | + virtual void service() |
| 44 | + { |
| 45 | + // If pin is on turn it off, otherwise turn it on |
| 46 | + _pinState = !_pinState; |
| 47 | + digitalWrite(_pin, _pinState); |
| 48 | + } |
| 49 | + |
| 50 | +private: |
| 51 | + bool _pinState; //the Current state of the pin |
| 52 | + int _pin; // The pin the LED is on |
| 53 | +}; |
| 54 | + |
| 55 | +Scheduler sched; // Create a global Scheduler object |
| 56 | + |
| 57 | +// Create our blink processes |
| 58 | +BlinkProcess blink250(sched, HIGH_PRIORITY, 250, 13); // Blink 13 every 250 ms |
| 59 | +BlinkProcess blink500(sched, HIGH_PRIORITY, 500, 12); // Blink 12 every 500 ms |
| 60 | +BlinkProcess blink1000(sched, HIGH_PRIORITY, 1000, 11); // Blink 11 every 1000 ms |
| 61 | + |
| 62 | +void setup() |
| 63 | +{ |
| 64 | + // Add and enable our blink processes |
| 65 | + blink250.add(true); // Same as calling blink250.add() and blink250.enable(); |
| 66 | + blink500.add(true); |
| 67 | + blink1000.add(true); |
| 68 | +} |
| 69 | + |
| 70 | +void loop() |
| 71 | +{ |
| 72 | + sched.run(); |
| 73 | +} |
0 commit comments