Little Robot Friends
Arduino Library Reference  version 1.1
MyLRF04: Event Handlers

Creating Custom Responses to Interaction Events

This tutorial uses example code found in:MyLRF04_EventHandlers.cpp

Up until now we’ve only been able to create expressions that play when our robot powers up, but what if we want our robot to make an expression when he’s ‘tapped’ or ‘tickled’? What about when the lights get dark or bright? For that we need to work with the LRFEvent system.

Before we get started, remember to call the LRF Library as well as include your setup() and loop():

#include “LRF.h”
void setup(void)
{
lrf.setup();
}
void loop(void)
{
lrf.loop();
}

You’ll also need to create your own expression, a light pattern and sounds, for the event handler:

// Create a light pattern for the eyes
LRFPattern myPattern = {
LRFColor_Blue, // starting color
LRFColor_Green, // target color
LRFPatternMode_FadeFlipToggle // pattern behaviour (see LRFUtils.h for more info)
};
// Create an array of sounds
LRFSound mySounds[3] = {
{ LRFNote_D, LRFOctave_4, LRFIntonation_Flat, LRFDuration_Long, LRFDuration_None } // <-- make sure the last entry has no comma!
};

In order to customize events we need to create an LRFEventHandler function, which is just a regular function with no parameters (inputs) or return values (outputs). Inside that code we can put whatever we want - but most ideally that will be a custom expression:

// Create a custom event handler function
void myTapHandler(void)
{
// let's blink and say our array of sounds
lrf.blinkAndSay(myPattern, mySounds, 3);
}

Now the setEventHandler() function needs to be placed in the void setup function, which will include the event and the event handler function:

void setup(void) // Arduino setup routine that gets called ONCE when the robot turns on
{
lrf.setup(); // lrf library should be set up before anything else
lrf.setEventHandler(LRFEvent_Tap, &myTapHandler); // let's connect our event handler to the tap event
}

The setEventHandler() will override the pre-made event handler already programmed in the robot and replace it with your own event handler function. It also connects the event handler to the event you’ve chosen. In this case we’ve connected our event handler to the tap event and whenever we tap or poke the hair on the robot they will react with the customized expression. If you want to change other types of event handlers check out the LRFEvent codes and the example code listed below to find out more.

All you need to do now is upload the code. After the Little Robot Friend has powered on, tap the hair at the top and see it react with your own customized expression. Try out different event types to see how they work.

All Done? Why Not Try This...

  • go through the list of events and attach your handler to different ones
  • try creating another custom function and call that from your event handler