Little Robot Friends
Arduino Library Reference  version 1.1
MyLRF05: More Event Handlers

Adding Multiple Event Handlers

This tutorial uses example code found in:MyLRF05_MoreEventHandlers.cpp

Now that you know how to create your own custom event handler, it’s time to make multiple event handlers so you can create your own set of unique responses for your Little Robot Friend.

Call the LRF Library and include the setup and loop:

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

Now create a few light patterns and sounds for the event handlers:

// Create a light pattern for the eyes
LRFPattern myPattern = {
LRFColor_Blue, // starting color
LRFColor_Green, // target color
LRFPatternMode_Fade // pattern behaviour (see LRFUtils.h for more info)
};
// Create another light pattern for our second handler
LRFPattern myOtherPattern = {
LRFColor_Pink, // starting color
LRFColor_Purple, // target color
LRFPatternMode_BoomerangToggle // 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!
};

As you can see, we’ve only created one array of sounds. This is because we’ll just be using the same sound for each event handler we’ll be making.

Now we just need to make our own event handler functions. Don’t forget to put in the expressions:

void myTapHandler(void)
{
// let's blink and say our array of sounds
lrf.blinkAndSay(myPattern, mySounds, 3);
}
void myTickleHandler(void)
{
// let's blink our other pattern with our sounds
lrf.blinkAndSay(myOtherPattern, mySounds, 3);
}

We’ve made two, just to show the difference in events. In the setup() we need to connect our event handlers:

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
// let's connect our event handlers
lrf.setEventHandler(LRFEvent_Tap, &myTapHandler);
lrf.setEventHandler(LRFEvent_Tickle, &myTickleHandler);
}

The events that we have chosen to customize are the tap, where you tap or poke the robot hair, and the tickle, where you tap or poke the hair multiple times. As you can see, in order to customize a different event you need to have a different setEventHandler() function for each one. Take a look at the LRFEvent for even more customizable events.

Now all you need to do is upload the code and test out the new event types you have created.

All Done? Why Not Try This...

  • create unique tap, tickle and hug event handlers
  • use the lightsOn and lightsOff events to customize the peekaboo game