Little Robot Friends
Arduino Library Reference
version 1.1
|
This tutorial uses example code found in:
MyLRF01_Blink.cppAlrighty - time to start programming your Little Robot Friend! A good place to begin is with a custom light pattern - a sequence of color that will play on the two RGB LED eyes. Before we begin with light patterns, and since this is the first tutorial, we should review one very important part of the LRF library AND a major part of writing an Arduino sketch - the setup() and loop() functions.
The setup()
function is used to set-up your sketch. You can use it to initialize variables (eg: myName = "fred"
) or call other functions that only need to be called once (eg: Serial.begin(9600)
).
The loop()
function is used for all your other code, and the loop function will be called over and over again - in a loop!
Every Arduino sketch needs these two essential functions in order to work. In fact, you can begin a brand new Arduino sketch with only:
However, with the LRF Library we need to use these two functions to call two essential functions of our own: lrf.setup()
and lrf.loop()
. These functions setup()
the LRF library and then loop()
all the other functions required to make your robot sense the world and express itself with blinks and chirps.
We explain this in more detail in the Firmware Basics section of the documentation.
On to making light patterns!
To create a unique light pattern you’ll need to get familiar with the LRFPattern object. This object is what is called a ‘structure’ or ‘struct’ in the C programming language. By generalizing what a pattern can do, we can create a series of simple codes that point to larger pieces of data. We do this with sets of enumerated constants (or enum in C) and look-up tables. This allows us to efficiently pack a lot of data into a relative small space (our LRFPattern structure), and lots of patterns into the limited space of our microcontroller.
Each LRFPattern is comprised of two LRFColor codes, a starting color, and a target color, and a LRFPatternMode code that describes the type of animation to use between the two values. Click on the links below to learn more about the LRFPattern and its options, LRFColor and LRFPatternMode.
Once we've created our pattern, we can call it using the API function lrf.blink(myPattern)
. This function takes our pattern variable myPattern
and blinks it. Your robot should begin with blue eyes, that quickly fade to green. Let's put this in our setup()
function so it happens when the robot starts up:
As you can see above, we've also added some small delays that will cause our sketch to pause for 1000 milliseconds (or 1 seconds).
Upload this code to your robot using the instructions at the end of the Installation section and check out the ideas section below for ideas on new patterns!
lrf.blink()
call in the loop()
- what happens now?