How to Program the Gorilla Bot

This lesson will go over how to program the Gorilla Bot

What it Does

The Gorilla Robot can be controlled using a remote controller; it can move forward/backward/right/left and speed up/down.

Objectives

Program the controller in such a way that:

  1. If up/down is pressed, move forward/backward.
  2. If right/left is pressed, move right/left.
  3. If F1 is pressed speed up.
  4. If F2 is pressed slow down.
  5. If C is pressed move forward at full-speed.
  6. Otherwise, stop.

To customize this robot further, the LEDs may be used as indicators of the speed levels. Starting with the four middle LEDs (13, 14, 15, 16) turned on, a decrease or increase in the speed level can be determined by decreasing or increasing the number of on LEDs respectively.

We will start with our setup code. Firstly we need to include the SmartInventor library. Then since we are using the motors and the remote control, we will need to set them up too.

#include <SmartInventor.h>

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();
}

void loop() {

}

Next we are going to add in the LEDs, so we need to initializes those as both inputs and have the starting LEDs lit. We will also create the variable to keep track of our current speed.

#include <SmartInventor.h>

int speed = 50;

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();

  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}

void loop() {

}

Next in the loop we need to read the remote and save which button was pressed so that we can later check to see what was pressed.

#include <SmartInventor.h>

int speed = 50;

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();

  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}

void loop() {
  int keyData = SmartInventor.TVRemoconData();
}

Now it is time to write the main section of the code. In order to do so, an “if statement” may be used to check whether any of controller buttons have been pressed. (Reminder! Click here to review how “if statements” work). The following statements checks whether any of the buttons have been pressed (Reminder! Click here to review how to program the remote controller) and makes a corresponding movement.

#include <SmartInventor.h>

int speed = 50;

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();

  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}

void loop() {
  int keyData = SmartInventor.TVRemoconData();

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
}

In order to program any other buttons another “else if statement” may be added to the above code. For example, to associate the center key of the controller with the robot going fast forward, the following code may be used:

#include <SmartInventor.h>

int speed = 50;

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();

  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}

void loop() {
  int keyData = SmartInventor.TVRemoconData();

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_C)
  {
    SmartInventor.DCMotor(M1, CCW, 127);
    SmartInventor.DCMotor(M2, CW, 127);
  }
}

In the case when no buttons are pressed, the following else can be used to bring the motor to a slow stop:

#include <SmartInventor.h>

int speed = 50;

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();

  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}

void loop() {
  int keyData = SmartInventor.TVRemoconData();

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_C)
  {
    SmartInventor.DCMotor(M1, CCW, 127);
    SmartInventor.DCMotor(M2, CW, 127);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0);
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }
}

In order to speed up or slow down the motors using F1 and F2 buttons, “if statements” must be used. In each case, the program must also check the speed before it increases or decreases the speed. This is to insure that the robot will not go too fast or too slow. We also added in a short delay so that the user has time to let go of the button before it changes again.

#include <SmartInventor.h>

int speed = 50;

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();

  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}

void loop() {
  int keyData = SmartInventor.TVRemoconData();

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_C)
  {
    SmartInventor.DCMotor(M1, CCW, 127);
    SmartInventor.DCMotor(M2, CW, 127);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0);
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }

  if(keyData == KEY_F1)
  {
    if(speed < 90)
    {
      speed = speed + 20;

      delay(150);
    }
  }
  else if(keyData == KEY_F2)
  {
    if(speed > 30)
    {
      speed = speed - 20;

      delay(150);
    }
  }
}

LEDs can be used to display how high or low the current speed is, meaning each time the speed is increased or decreased two more LEDs should be turned on or off accordingly. This will allow the user to see what speed it will be moving at. Also we will add in a beep to let us know when it changed speeds.

#include <SmartInventor.h>

int speed = 50;

int speedMeterL = 13;
int speedMeterR = 16;

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();

  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}

void loop() {
  int keyData = SmartInventor.TVRemoconData();

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_C)
  {
    SmartInventor.DCMotor(M1, CCW, 127);
    SmartInventor.DCMotor(M2, CW, 127);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0);
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }

  if(keyData == KEY_F1)
  {
    if(speed < 90)
    {
      speed = speed + 20;

      speedMeterL = speedMeterL - 1; // light up two more LEDs 
      speedMeterR = speedMeterR + 1; 

      digitalWrite(speedMeterL, HIGH);
      digitalWrite(speedMeterR, HIGH);

      SmartInventor.Buzz(1160, 12);

      delay(150);
    }
  }
  else if(keyData == KEY_F2)
  {
    if(speed > 30)
    {
      speed = speed - 20;

      digitalWrite(speedMeterL, LOW); // turn off the outer two leds
      digitalWrite(speedMeterR, LOW);

      speedMeterL = speedMeterL + 1;
      speedMeterR = speedMeterR - 1;

      SmartInventor.Buzz(1160, 12);

      delay(150);
    }
  }
}

Final Code

#include <SmartInventor.h>

int speed = 50;

int speedMeterL = 13;
int speedMeterR = 16;

void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();

  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);

  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}

void loop() {
  int keyData = SmartInventor.TVRemoconData();

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed);
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_C)
  {
    SmartInventor.DCMotor(M1, CCW, 127);
    SmartInventor.DCMotor(M2, CW, 127);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0);
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }

  if(keyData == KEY_F1)
  {
    if(speed < 90)
    {
      speed = speed + 20;

      speedMeterL = speedMeterL - 1; // light up two more LEDs 
      speedMeterR = speedMeterR + 1; 

      digitalWrite(speedMeterL, HIGH);
      digitalWrite(speedMeterR, HIGH);

      SmartInventor.Buzz(1160, 12);

      delay(150);
    }
  }
  else if(keyData == KEY_F2)
  {
    if(speed > 30)
    {
      speed = speed - 20;

      digitalWrite(speedMeterL, LOW); // turn off the outer two leds
      digitalWrite(speedMeterR, LOW);

      speedMeterL = speedMeterL + 1;
      speedMeterR = speedMeterR - 1;

      SmartInventor.Buzz(1160, 12);

      delay(150);
    }
  }
}