From Youtube: Arduino Event-Based Programming (Source, etc: github.com/johnnyb/)
// =================================================================== // demonstrate event driven programming // (for every event we want a function) // ------------------------------------------------------------------- //What it does: // blink LED (on/off) controlled by switch // ------------------------------------------------------------------- // From: https://www.youtube.com/watch?v=QvQAl-zbiCI // Arduino Event-Based Programming // ------------------------------------------------------------------- // Source code, etc: github.com/johnnyb // =================================================================== # include <Eventually.h> # define LEDPIN 5 # define SWITCHPIN 2 EvtManager mgr; // ------------------------------------------------------------------- // ---- setup // ------------------------------------------------------------------- void setup() { pinMode(LEDPIN,OUTPUT); pinMode(2,OUTPUT); // ----------------------------------------------------------------- // could add listeners here but it is also done in startBlink // so just do startBlink // add listener to time, every 1000 miliseconds call function // add pin listener, start/stop blinking LED // mgr.addListener(new EvtTimeListener(1000,true,(EvtAction)BlinkLight)); // mgr.addListener(new EvtPinListener(SWITCHPIN,(EvtAction)stopBlink)); // ----------------------------------------------------------------- startBlink(); } // ------------------------------------------------------------------- // ---- blink LED // ------------------------------------------------------------------- bool blinkstate = false; bool blinkLight() { if (blinkstate) { blinkstate = false; digitalWrite(LEDPIN,LOW); } else { blinkstate = true; digitalWrite(LEDPIN,HIGH); } return false; } // ------------------------------------------------------------------- // ---- stop blinking LED // ------------------------------------------------------------------- bool stopBlink() { mgr.resetContext(); // remove all listeners mgr.addListener(new EvtPinListener(2,(EvtAction)startBlink)); return true; // stop event cycle } // ------------------------------------------------------------------- // ---- start blinking LED // ------------------------------------------------------------------- bool startBlink() { mgr.resetContext(); // remove all listeners // add listener to time, every 1000 miliseconds call function // add pin listener, start blinking LED mgr.addListener(new EvtTimeListener(1000, true, (EvtAction)blinkLight)); mgr.addListener(new EvtPinListener(SWITCHPIN, (EvtAction)stopBlink)); return true; // stop event cycle } USE_EVENTUALLY_LOOP(mgr)