How to Program the Bug Bot

This lesson will go over how to program the Bug Bot

What it Does

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


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.

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.
Next we need to add in the code for the LEDs. We need to set them up as outputs so that we can control the LEDs. Then we will also turn the middle four LEDs on as our starting value.
Next inside of void loop we will read the remote and save the value of it so that we can use it later.
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 move in the correct direction (up is forward, right is turn right, ect.).
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:
In the case when no buttons are pressed, the following can be used to bring the motor to a slow stop:
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 first check the speed to be sure that it will not be too slow or too fast and then increase or decrease the speed by 20. It will then have a short wait to allow the user to have time to either let go of the button or keep pressing it.
LEDs can be used to display how high or low the current speed is, meaning each time the speed is increased/decreased two more LEDs should be turned on/off accordingly. We will use two variables to keep track of the outer most LEDs that are lit up.

Final Code