Mission Objective 2 – Guidance and Code

Things that must be coded:

Servo Motors:

  • Move forward / backward
  • Steer
  • Turn

9-DOF Sensor for heading:

  • Use the 9-DOF library to obtain your initial heading
  • Must determine an adequate method to determine your initial heading
  • Use a push button to start the rover and set the heading then

Ultrasonic Sensor:

  • Attach the ultrasonic sensor to the rover
  • Pins to use are 5V for power, GND for ground and Signal to a digital I/O pin
  • Create a function to calculate distance based on the “chirp” sent and echo received


Servo Motors

To move forward (or backward):

int moveFast = 7;
int moveSlow = 4;

rightServo.write(90 + moveFast);
leftServo.write(90 – moveFast);


To steer, make one side move faster than the other.

int moveFast = 7;
int moveSlow = 4;
int steer = 2;

// steer left (make right side go faster)
rightServo.write(90 + moveFast + steer);
leftServo.write(90 - moveFast );

// steer right (make left side go faster)
rightServo.write(90 + moveFast);
leftServo.write(90 – moveFast – steer);


To turn in one place:

int turnSpeed = 3;

// Turn right
rightServo.write(90 + turnSpeed);
leftServo.write(90 + turnSpeed);

// Turn left
rightServo.write(90 – turnSpeed);
leftServo.write(90 – turnSpeed);


9-DOF Sensor

Get current heading:

double currentHeading;

dof.magGetOrientation(SENSOR_AXIS_Z, &magEvent, &orientation);
currentHeading = orientation.heading;


Ultrasonic Sensor

Send chirp and listen to echo

int sonarPin = 5;

// Send out a chirp
// Clear the pin first, then send out the chirp
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(sonarPin, OUTPUT);
digitalWrite(sonarPin, LOW);
digitalWrite(sonarPin, HIGH);
digitalWrite(sonarPin, LOW);
// Reading echo
pinMode(sonarPin, INPUT);
long duration = pulseIn(sonarPin, HIGH); // Time in microseconds

// Calculate the distance
// Speed of sound = 340 m/s or 29 microseconds/cm
// Ping travels out and back, therefore distance is half the time


Getting the Range

Range is calculated according to the speed of sound. The speed of sound is defined as 340 m/s or 29 microseconds/cm. The chirp from the ultrasonic sensor goes out, bounces off the object and comes back. The distance the sound wave travels will be double the distance to the object. So, make sure to account for that the distance traveled.



  1. Use a push button to set boolean variable to indicate rover is moving (e.g. isRoverStarted). This is a “flag” used to check if the rover is in a start state.
    • Get the current heading and store that value in a variable called startHeading. This will be the heading that the rover will always try to maintain
    • Push button is only used once. It just sets the isRoverStarted boolean to true and also sets the startHeading.
  2. Implement a second push button to stop the rover (i.e. set the isRoverStarted boolean variable to false.
  3. If isRoverStarted is true
    • Move forward
    • Check if an object in the way. An object is in the way if the ultrasonic sensor returns a distance to the object that is within your threshold.
    • Stop if object is in the way.
    • Move forward no object is in the way
    • Get currentHeading
    • Check if the currentHeading differs from the startHeading by your navigation threshold. If beyond threshold, apply steering factor to adjust heading.