First off, I only recently started experimenting with Arduino and C++. For the last couple of years I have been mostly involved with languages such as PHP, so it’s a rather big change going from a dynamic language to a structured language such as C++.

For my current Arduino project I am attempting to create a remote control car that will have headlights, steering and off course…a drive-train. Having written an article about “Event Management” in PHP quite recently, I thought that this will be a great pattern to use in my Arduino sketch. I have all these different components to control and instead of doing it with a bunch of if statements in my loop() function, I opted for an event-driven approach.

To accomplish this, I wrote a new library for Arduino. It’s simple called “Event”…and can be downloaded from Github. I will be using this library as the basis for my examples.

What I want to accomplish in this article is to set up events to turn a LED on and off much like I did in my article explaining on how to use the Serial Port.

 1 #include "Event.h"
 3 EventManager evtManager;
 4 int ledPin = 12;
 6 // Set up the LED task
 7 struct LEDListener : public EventTask
 8 {
 9     using EventTask::execute;
11     void execute(Event evt)
12     {
13         digitalWrite(ledPin, HIGH);
14     }
16 } LEDListener;
19 // Run the sketch
20 void setup()
21 {
22     pinMode(ledPin, OUTPUT);
23     evtManager.subscribe(Subscriber("led.on", &LEDListener));
24 }

To create a new listener, we simple extend the EventTask struct and override it’s execute method. For this example we are manipulating an LED, so we can either define the pin and the output in the LEDlistener struct or define it in the setup (like we did in the example). When writing your own sketch you will probably have a library to deal with LED’s, instead of defining the pins and modes in your root sketch file.

Now that our listener is a subscriber to the required event, we actually need to trigger this event.

1 ...
3 void loop()
4 {
5     // Trigger led event
6     Event ledEvent = new Event("led.on");
7     evtManager.trigger(ledEvent);
8 }

For the purpose of this example, we are going to trigger the LED event in the loop() function. You probably shouldn’t trigger an event that does the same thing on every loop() cycle. ;)

Regardless, the LED should turn on when you fire up this sketch. If it doesn’t, feel free to drop a comment below and I will try to assist. So…before we conclude this article, let’s have a look at how we can easily make a “Blinking LED” program without using any delay() commands.

 1 #include "Event.h"
 3 EventManager evtManager;
 4 int ledPin = 12;
 5 boolean ledOn = false;
 7 // Set up the LED task
 8 struct LEDListener : public EventTask
 9 {
10     using EventTask::execute;
12     void execute(Event evt)
13     {
14         if (ledOn == true)
15         {
16             digitalWrite(ledPin, LOW);
17         }
18         else
19         {
20             digitalWrite(ledPin, HIGH);
21         }
22     }
24 } LEDListener;
27 // Run the sketch
28 void setup()
29 {
30     pinMode(ledPin, OUTPUT);
31     evtManager.subscribe(Subscriber("led.on", &LEDListener));
33     Event toggleLed = Event("led.on");
34     evtManager.triggerInterval(TimedTask(2000, toggleLed));
35 }
37 void loop()
38 {
39     evtManager.tick();
40 }

The important changes to note, is the use of triggerInterval(). This tells the code to trigger a certain event every X milliseconds (2 seconds in this case). You also need to ensure to call tick() on the EventManager for every loop() cycle. This tells the EventManager where in time it is. Without calling tick(), timed events won’t work.

If you encounter any problems with the code examples or think you can contribute, please feel free to comment below or fork it on Github.