1

Program the Bug Bot

Just like the Trailer Bot and Gorilla Bot, the Bug Bot can be used with a remote control! The Bug Bot can move forward, backward, right, and left. It can also and speed up, slow down, move forward at full speed, and stop.

This is the Bug Bot’s pseudocode:

  • If up is pressed on the remote control, move forward.
  • Else, if down is pressed, move backward.
  • Else, if right is pressed, move right.
  • Else, if left is pressed, move left.
  • If F1 is pressed, speed up.
  • Else, if F2 is pressed slow down.
  • Else, if C is pressed, move forward at full speed.
  • Else, stop.

The first thing you need to do is include the Smart Inventor library before void setup(). Next, inside void setup(), include your motors and remote control.

#include <SmartInventor.h>
 
void setup() {
  SmartInventor.DCMotorUse();
  SmartInventor.TVRemoconUse();
}

Just like the other remote controlled bots, you can program your LEDs to display how fast or slow your Bug Bot is moving! First, set up a speed variable underneath the Smart Inventor library, and then set variables for the LEDs so they can display speed later on. In void setup(), use pinMode to set the LEDs as outputs and use digitalWrite to make sure pins 13 through 16 are on.

#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);
}

Create a variable to read the remote control and store information about the last button that was pressed. You want this information to be read continuously, so make sure it’s in void loop().

#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();
}

Next up: the main section of the code! Use if, else if, and else statements to check if any of the remote control’s buttons have been pressed. If they have, the motors will turn in that direction. For example, if you press the left button, the motors will spin in the same direction to move left.

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);
  }
}

If you want to add more buttons, you can just use more else if statements in your code. For example, adding the center button on the remote control (KEY_C in your code) will make your Bug Bot move at full speed!

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);
  }
}

What if you want your Bug Bot to slow down? You can add an else statement to the end of your code to bring your Bug Bot to a slow stop when no buttons are pressed.

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);
  }
}

Your Bug Bot has more speeds besides full speed and slow stop. To include your remote control’s F1 and F2 buttons, which will speed up or slow down the motors, use if and else if statements after your code for the other buttons. You should also add in short delays so you have time to let go of the remote control’s button before it changes again.

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);
    }
  }
}

The middle LEDs (13-16) on your Smart Inventor Board can be programmed to display your Bug Bot’s speed. Every time that the speed increases or decreases, two LEDs will turn on or off.

#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;
         // light up two more LEDs 
      speedMeterL = speedMeterL - 1; 
      speedMeterR = speedMeterR + 1; 
 
      digitalWrite(speedMeterL, HIGH);
      digitalWrite(speedMeterR, HIGH);
 
      delay(150);
    }
  }
  else if(keyData == KEY_F2)
  {
    if(speed > 30)
    {
      speed = speed - 20;
         // turn off the outer two LEDs
      digitalWrite(speedMeterL, LOW); 
      digitalWrite(speedMeterR, LOW);
 
      speedMeterL = speedMeterL + 1;
      speedMeterR = speedMeterR - 1;
 
      delay(150);
    }
  }
}

Your final code should look something like this:

#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);       

            delay(150);    
        }  
    }  

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

            speedMeterL = speedMeterL + 1;   // turn off the outer two leds   
            speedMeterR = speedMeterR - 1;


            digitalWrite(speedMeterL, LOW);       
            digitalWrite(speedMeterR, LOW);       

            delay(150);    
        }  
    }
}

Still don’t understand the Bug Bot or want to check your program? Watch the video!