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.
[crayon-6758b3df38c8d453192322/]
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.
[crayon-6758b3df38c95714024429/]
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).
[crayon-6758b3df38c98415757921/]
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.
[crayon-6758b3df38c9b224768461/]
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.
[crayon-6758b3df38c9c064895667/]

Final Code

[crayon-6758b3df38c9d027821397/]