RobotControl Basic movements Example

Basic movements example

This example shows you how to program the movement of your Robot. 
You can save until a sequence of 20 steps. 
First upload this sketch on Robot. 
After press the buttons of control on board to indicate the direction that the Robot will move. 
Type the commands of Arduino Robot via directional buttons. 
Finally press the middle button to record the steps and the Robot will move following the steps inserted by you. 
Arduino Robot follows your commands 
The Robot executes each command for one second.

Hardware Required

  • Arduino Robot
  • SD-card


The detailed instructions are shown down:

  • Upload this sketch.
  • Unplug USB.
  • Turn on power.
  • Place the robot on the floor.
  • Uploaded the sketch, the robot will ask you to add commands.
  • Press the buttons on console according to how you want the robot to move; the commands will be stored and executed later.
  • Finally press the middle button. The robot will start executing the commands in the inserting order.

The Robot can record max 20 commands, if you fill them all, the robot will start executing before you press the middle button. 
After running the sequence, the robot will be ready to be programed again.


The complete code and its detailed description are shown down.

/* Robot Logo
This example uses images on an SD card. It looks for
files named "lg0.bmp" and "lg1.bmp" and draws them on the
* Arduino Robot
created 1 May 2013
by X. Yang
modified 12 May 2013
by D. Cuartielles
This example is in the public domain
#include <ArduinoRobot.h> // include the robot library
#include <Wire.h>
#include <SPI.h>
int commands[20]; // array to store commands
void setup() {
Robot.begin();// initialize the Robot
Robot.beginTFT();// initialize the display
Robot.beginSD();// initialize SD-card
Robot.displayLogos();// draw "lg0.bmp" and "lg1.bmp" on the screen
void loop() {
Robot.drawBMP("intro.bmp", 0, 0); //display background image
iniCommands(); // remove commands from the array
addCommands(); // add commands to the array
delay(1000); // wait for a second
executeCommands(); // follow orders
Robot.stroke(0, 0, 0);
Robot.text("Done!", 5, 103); // write some text to the display
delay(1500); // wait for a moment
// empty the commands array
void iniCommands() {
for (int i = 0; i < 20; i++)
  commands[i] = -1;
// add commands to the array
void addCommands() {
Robot.stroke(0, 0, 0);
// display text on the screen
Robot.text("1. Press buttons to\n add commands.\n\n 2. Middle to finish.", 5, 5);
// read the buttons' state
for (int i = 0; i < 20;) { //max 20 commands
  int key = Robot.keyboardRead();
  if (key == BUTTON_MIDDLE) { //finish input
  } else if (key == BUTTON_NONE) { //if no button is pressed
  commands[i] = key; // save the button to the array
  PrintCommandI(i, 46); // print the command on the screen
// run through the array and move the robot
void executeCommands() {
// print status to the screen
Robot.text("Excuting...", 5, 70);
// read through the array and move accordingly
for (int i = 0; i < 20; i++) {
  switch (commands[i]) {
    case BUTTON_LEFT:
      Robot.turn(-90); // turn left
    case BUTTON_RIGHT:
      Robot.turn(90); // turn right
    case BUTTON_UP:
      Robot.motorsWrite(255, 255); //move forward
    case BUTTON_DOWN:
      Robot.motorsWrite(-255, -255); //move back
    case BUTTON_NONE:
  // print the current command to the screen
  Robot.stroke(255, 0, 0);
  PrintCommandI(i, 86);
  // stop moving for a second
// convert the button press to a single character
char keyToChar(int key) {
switch (key) {
    return '<';
    return '>';
  case BUTTON_UP:
    return '^';
    return 'v';
// display a command
void PrintCommandI(int i, int originY) {
Robot.text(keyToChar(commands[i]), i % 14 * 8 + 5, i / 14 * 10 + originY);

You have need of Wire.h, SPI.h and ArduinoRobot.h libraries to use this sketch.