How to Program the Soccer Bot

This lesson will go over how to program the Soccer Bot

What it does

The Soccer Robot can be controlled using a remote controller; it can move forward and backward and also turn to the right and left.

Objective

Program the controller in such a way that:

  1. If up is pressed, move forward.
  2. If down is pressed, move backward.
  3. If left is pressed, turn left.
  4. If right is pressed, turn right.
  5. Otherwise, stop.

Now we need to work on the setup code. We need to include the SmartInventor library to pull in all of the background files. Then since we are using the motors and the remote we will need to set both of them up.

#include <SmartInventor.h>

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

void loop() {

}

Next in void loop we will read the remote and save which button is pressed to allow us to check later.

#include <SmartInventor.h>

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

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.

The following statement checks whether the up button has been pressed. If it is, it will drive forward.

#include <SmartInventor.h>

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

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

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, 50); // Move forward
    SmartInventor.DCMotor(M2, CW, 50);
  }
}

Similarly for when down, left, or right buttons are pressed, we will move accordingly:

#include <SmartInventor.h>

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

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

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, 50); // Move forwards
    SmartInventor.DCMotor(M2, CW, 50);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, 50); // Move backwards
    SmartInventor.DCMotor(M2, CCW, 50);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, 50); // Turn left
    SmartInventor.DCMotor(M2, CCW, 50);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, 50); // Turn right
    SmartInventor.DCMotor(M2, CW, 50);
  }
}

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

#include <SmartInventor.h>

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

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

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, 50); // Move forwards
    SmartInventor.DCMotor(M2, CW, 50);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, 50); // Move backwards
    SmartInventor.DCMotor(M2, CCW, 50);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, 50); // Turn left
    SmartInventor.DCMotor(M2, CCW, 50);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, 50); // Turn right
    SmartInventor.DCMotor(M2, CW, 50);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0); // Stop
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }
}

Optional: In order to customize the robot’s actions, the motor speed or the delay time may be changed accordingly.

Final Code

#include <SmartInventor.h>

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

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

  if(keyData == KEY_U)
  {
    SmartInventor.DCMotor(M1, CCW, 50); // Move forwards
    SmartInventor.DCMotor(M2, CW, 50);
  }
  else if(keyData == KEY_D)
  {
    SmartInventor.DCMotor(M1, CW, 50); // Move backwards
    SmartInventor.DCMotor(M2, CCW, 50);
  }
  else if(keyData == KEY_L)
  {
    SmartInventor.DCMotor(M1, CCW, 50); // Turn left
    SmartInventor.DCMotor(M2, CCW, 50);
  }
  else if(keyData == KEY_R)
  {
    SmartInventor.DCMotor(M1, CW, 50); // Turn right
    SmartInventor.DCMotor(M2, CW, 50);
  }
  else
  {
    SmartInventor.DCMotor(M1, LOOSE, 0); // Stop
    SmartInventor.DCMotor(M2, LOOSE, 0);
  }
}