Part 1: How to Program the Truck Bot

This lesson will go over how to program the first part of the Truck Bot

Part 2: How to Program the Truck Bot

This lesson will go over how to program the second part of the Truck Bot

What it does?

The Truck Robot can be controlled using a remote controller; it can move forward/backward, curve to the 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, curve right/left.
  3. If F1 is pressed speed up.
  4. If F2 is pressed slow down.
  5. 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. Also we are going to add in the LEDs, so we need to intializes those as well.

#include <SmartInventor.h>

int speed = 60;

int speedMeterL = 13;
int speedMeterR = 16;

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

  pinMode(11, OUTPUT); // setup the LEDs
  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 = 60;

int speedMeterL = 13;
int speedMeterR = 16;

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

  pinMode(11, OUTPUT); // setup the LEDs
  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 button has been pressed (Reminder! Click here to review how to program the remote controller) and if any were it will move in a corresponding direction (much like the soccer bot).

#include <SmartInventor.h>

int speed = 60;

int speedMeterL = 13;
int speedMeterR = 16;

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

  pinMode(11, OUTPUT); // setup the LEDs
  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); // Move forwards
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed); // Move backwards
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed); // Turn left
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed); // Turn right
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0); // Stop
    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 = 60;

int speedMeterL = 13;
int speedMeterR = 16;

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

  pinMode(11, OUTPUT); // setup the LEDs
  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); // Move forwards
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed); // Move backwards
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed); // Turn left
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed); // Turn right
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0); // Stop
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }

  if(keyData == KEY_F1)
  {
    if(speed < 90) // make sure we are not going too fast
    {
      speed = speed + 20; // speed up

      delay(150);
    }
  }
  else if(keyData == KEY_F2)
  {
    if(speed > 30) // make sure we are not going too slow
    {
      speed = speed - 20; // slow down

      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.

#include <SmartInventor.h>

int speed = 60;

int speedMeterL = 13;
int speedMeterR = 16;

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

  pinMode(11, OUTPUT); // setup the LEDs
  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); // Move forwards
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed); // Move backwards
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed); // Turn left
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed); // Turn right
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0); // Stop
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }

  if(keyData == KEY_F1)
  {
    if(speed < 90) // make sure we are not going too fast
    {
      speed = speed + 20; // speed up

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

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

      delay(150);
    }
  }
  else if(keyData == KEY_F2)
  {
    if(speed > 30) // make sure we are not going too slow
    {
      speed = speed - 20; // slow down

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

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

      delay(150);
    }
  }
}

Final Code

#include <SmartInventor.h>

int speed = 60;

int speedMeterL = 13;
int speedMeterR = 16;

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

  pinMode(11, OUTPUT); // setup the LEDs
  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); // Move forwards
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, speed); // Move backwards
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, speed); // Turn left
    SmartInventor.DCMotor(M2, CCW, speed);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, speed); // Turn right
    SmartInventor.DCMotor(M2, CW, speed);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0); // Stop
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }

  if(keyData == KEY_F1)
  {
    if(speed < 90) // make sure we are not going too fast
    {
      speed = speed + 20; // speed up

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

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

      delay(150);
    }
  }
  else if(keyData == KEY_F2)
  {
    if(speed > 30) // make sure we are not going too fast
    {
      speed = speed - 20; // slow down

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

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

      delay(150);
    }
  }
}