What are Events and Event Handlers?

20m

What are events?

Every time a sensor on your robot is interacted with, an event gets triggered. “Event” is a programming term for “when something happens”.

In the real world, we respond to events all the time! Think about when the alarm goes off in the morning to wake you up, that’s an event that we react to! Well, hopefully, by waking up. If someone taps your shoulder, that’s an event that we react to by turning around. Similarly to this, the robot reacts to changes in its environment. These changes are detected as “events”. For example, when you tap your robot’s hair sensor, the “LRFEvent_Tap” event gets triggered, or if you cover your robot’s light sensor, the “LRFEvent_LightDark” event gets triggered and so on.

The Little Robot Friend’s library uses the event system to control the robot’s behaviour, it also makes it easier for you to program your robot’s reaction to different inputs.

What are event handlers ?

“Event handler” is the term given to the block of code that gets executed when a particular event gets triggered. Every event has a built-in event handler in the Little Robot Friends library. For example, when the “LRFEvent_Tap” event gets fired after you tap your robot’s hair sensor, a built-in event handler instructs the robot to express the “Hello” expression. There is a matching event handler for every event.

Mapping events and event handlers

In order to overwrite an existing event handler, we need to link the event to the new event handler. This is done through a simple function: lrf.setEventHandler(). This function is run in the setup block of code and requires two inputs: the name of the event and the name of the event handler. Don’t worry if this is confusing, we’re about to show you how it all goes together…

How to put it all together?

Now that we’ve looked at what makes up events, let’s check out an example that demonstrates how to use them!

Declare the event handler

#include <LittleRobotFriends.h>

void myTapHandler(LRFEvent event)
{
   lrf.blinkAndSayPreset(LRFExpressionName_Excited);
}

So, first things first, we declare our event handler at the top of the sketch. This sketch doesn’t have any variables so we just start writing the event handler underneath the include statement. Within the event handler, there is the blinkAndSayPreset function which will play the excited expression.

Map the event to the event handler

void setup()
{
  // set up our library
  lrf.setup();

  // set an event handler for when we tap our hair
  lrf.setEventHandler(LRFEvent_Tap, myTapHandler);
}

As usual, we need to run our lrf.setup() function from within the setup block of code. After the library is set up we need to override the existing event handler for the LRFEvent_Tap event. This is done by using lrf.setEventHandler() function. It takes two inputs, first is the event - in this case the tap event. The second input is the name of our event handler that we declared previously, in this example it’s called “myTapHandler”.

Run the loop as usual.

void loop()
{
  // service our library loop
  lrf.loop();
}

Even though we are using an event to determine when our code is run, we still need to have a loop block and within that, run the lrf.loop() function to make sure our sensor values are updated.

Here’s the complete sketch.

#include <LittleRobotFriends.h>

void myTapHandler(LRFEvent event)  //declare the event handler
{

 lrf.blinkAndSayPreset(LRFExpressionName_Excited);  //run our code

}

void setup()
{

  // set up our library
  lrf.setup();

  // set an event handler for when we tap our hair
  lrf.setEventHandler(LRFEvent_Tap, myTapHandler);

}

void loop()
{

  // service our library loop
  lrf.loop();

}