How to read an Arduino sketch.

10m

Introduction

Arduino sketches are made up of plain text, they are a collection of code, written out and stored in an .ino file. The sketch is written in text but when we upload the code to the robot, it is converted into a format that the robot can understand. We write the code in text because it’s a whole lot easier than typing out thousands of 1s and 0s, which is really the robot’s native language.

In order to better understand the Little Robot Friends Arduino code and our examples, here is a breakdown of the anatomy of a vanilla Arduino sketch. All Arduino sketches are comprised of at least these parts:

  • The Setup.

    The setup block of code is where we put everything we want to happen when the robot is first turned on. It only runs once, at the start. It’s a great place to set settings and initialise values.

  • The Loop.

    Whereas the setup block of code only gets executed once, the loop block of code is executed repeatedly. As long as there is power and the robot is turned on, the loop block will run over and over again. This happens at an incredible speed, many, many times per second. This is a good place to have values change over time to effect the robot.

Anatomy of a basic LRF Arduino Sketch

So now that you know what a regular Arduino sketch looks like, let’s have a look at what a Little Robot Friends Arduino sketch is made up of.

There are a couple of requirements that are necessary for you to include in the sketch in order for the robot to function correctly. These should always be included in all sketches that you upload to the robot.

Include the Library

#include <LittleRobotFriends.h>

The first required step is to import the Little Robot Friends Arduino library. The Little Robot Friends library is a collection of code that we have provided that allows you to easily reprogram your robot using functions that we have designed.

The idea behind a library is that it makes it a lot easier for you to do things using code that already exists. The library is imported at the top of the sketch, using the import keyword.

lrf.setup

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

Within the setup block of code, we have lrf.setup() - this function starts up the lrf library and initiates the robot’s settings. It’s very important and always needs to be included with any sketch that you upload to your robot.

lrf.loop

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

The lrf.loop() function is called within the loop block of code. As mentioned before, everything in this block is executed many times per second. lrf.loop() is responsible for “servicing” the Little Robot Friends library, this means that it ensures that the robot is checking its sensors and able to react to input. Without this line of code, your robot will not be able to sense and act in the world around it. It must be included in every sketch that you upload to your robot.

Put It Together

#include <LittleRobotFriends.h>

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

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

Real World Example

So far we have looked at what makes up a regular Arduino sketch and what is required when writing a Little Robot Friends Arduino program. These sketches have the bare minimum of what can be included in a sketch, now let’s look at an example that is a more realistic to what you’ll see in a sketch.

Below is an example of a program that turns the Little Robot Friend robot into a flashlight. Have a look at it and see if you can follow along with what it does!


#include <LittleRobotFriends.h>       //import LRF library

boolean flashLightToggle = false;     //variable to store if lights are on or off

void setup() {
  lrf.setup();                        //setup LRF library
  lrf.setBoredomTimer(0);             //disable boredom and sleep
  lrf.disableEventExpressions();                //disable other events so that robot only responds to set events below
  lrf.setEventHandler(LRFEvent_Tap, &myTapHandler);  //Setting up tap handler function
}

void myTapHandler(LRFEvent event)         //custom event handler
{
flashLightToggle = !flashLightToggle;     //flip the value of the lights from off to on and vice a versa.
}

void loop() {
  lrf.loop();                 //service LRF loop

if (flashLightToggle == true){            //check value of toggle and turn on lights if true
   lrf.eyes.set(250, 250, 250);           //set LRF LEDs to full brightness
}
else if (flashLightToggle == false){      //check value of toggle and turn lights off if false
   lrf.eyes.setColor(LRFColor_Clear);      //clear LRF LEDs (turn off)
}
}