Moving your robot

I should imagine you’ll probably want to get your robot moving, no? Well let’s see how to do that:

#include <Servo.h>
#include <DFRduino.h>


DFRduino robot;

void setup() {
}

void loop() {
  // Set the robot's direction forward
  robot.setDirection('f');
}

Compile and upload the code to your robot, hopefully it should go forward. Now, if your robot is anything like mine (I hope it isn’t, it’s had many many problems!) it may drift to the left, or perhaps to the right. Now the function setDirection is not of much use as you can’t compensate for this hardware asymmetry. You’ll have to write your own function that replicates setDirection to make your robot go straight. To set the speed of the left and right motors, I’ve provided you with another function called setMotors(int left, int right). Here I show my version of setDirection:

#include <Servo.h>
#include <DFRduino.h>


DFRduino robot;

void setup() {
}

void loop() {
  // Set the robot's direction forward
  mySetDirection('f');
}

void mySetDirection(char direction) {
  switch(direction) {
    case 'f':
      // Compensate for drifting left by reducing
      // the power of the right motor.
      robot.setMotors(100, 90);
      // If you don't break out of your switch statements,
      //  all the other cases will also be run.
      break;
    case 'b':
      robot.setMotors(-100, -90);
      break;
    case 'r':
      robot.setMotors(100, 0);
      break;
    case 'l':
      robot.setMotors(0, 100);
      break;
    default:
      // Provide some way of checking for errors in your 
      // calling code. This will be called if you don't call your
      // function with 'f', 'b', 'r' or 'l'.
      Serial.println("Direction not recognised");
      break;
  }
}

You can set your motor power between -100 (maximum speed backwards) and 100 (maximum speed forwards).

If you’re not comfortable with switch statements, you could have 4 separate functions that do forward, backward, left, and right instead.

// Write four functions like this
void setMotorsForward() {
  robot.setMotors(100, 90);
}

It’s up to you which style you prefer, I have a slight preference for keeping very similar behaviour all within one function.

Now you’ve written a function to get your robot moving, or perhaps your robot works perfectly fine and you didn’t need to write any of your own functions, we can start making the robot do something a little more interesting.

Let’s make the robot trace out a square on the floor.

#include <Servo.h>
#include <DFRduino.h>


DFRduino robot;
// How long shall we move forward for? (in milliseconds)
int delayTime = 1000;
// This depends on how charged your batteries are. This works for me on 
// new batteries
int timeTakenToMoveNinetyDegrees = 350;

void setup() {
}

void loop() {
  // Move forward
  robot.setDirection('f');
  delay(delayTime);
  // Turn right
  robot.setDirection('r');
  // wait until we've rotated 90 degrees
  delay(timeTakenToMoveNinetyDegrees);
  // When we loop again, we'll go forward
}

As you may have noticed when modifying timeTakenToMoveNinetyDegrees your robot will have traced out different polygons on the floor. Your robot wouldn’t have been turning 90 degrees, it would have been more or less, so it might have traced out octagons (if you had very uncharged batteries) or maybe even a triangle with charged batteries.

Take some time to play around with the functions I’ve provided you: setMotors and setDirection; and any you have written yourself like mySetDirection.

Once you’re comfortable directing your robot you should move on to sensing.