Building the HUB-ee Buggy

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

Contributors: Nick Poole
Favorited Favorite 4

Basic Object Avoidance

The HUB-ee Buggy is almost ready to go, but right now it doesn't have any way of avoiding obstacles. We need to tell it what to do when it encounters a wall or other large object. There are a lot of really clever ways to do this, and you can make the code as complicated as you want. In this case, though, we'll keep it basic. The HUB-ee Buggy will keep an eye out for obstacles and turn around when it gets too close to one. This will be achieved by reversing one of the wheels for a few milliseconds.

Plug the Redboard into your computer, and load up this Arduino code:


// Define the pins to make the code more readable

// Our Sharp IR sensor is connected to A4
#define sharpIR A4

// Our left HUB-ee is connected to D6, D7 and D8
#define leftPWM 6
#define leftIN1 7
#define leftIN2 8

// Our right HUB-ee is connected to D9, D10 and D11
#define rightPWM 9
#define rightIN1 10
#define rightIN2 11

// This is the threshold for how close an object will
// be allowed to get before the HUB-ee Buggy changes
// its direction of travel. You may need to adjust it.
int tooClose = 200;

// This stores the value read from the Sharp IR sensor
int irVal = 0;

void setup() {

  // Set the infrared sensor as an input. This isn't
  // strictly necessary because the ADC is an input
  // by default but it's good practice.
  pinMode(sharpIR, INPUT);

  // Set all of the outputs accordingly
  pinMode(leftPWM, OUTPUT);
  pinMode(leftIN1, OUTPUT);
  pinMode(leftIN2, OUTPUT);

  pinMode(rightPWM, OUTPUT);
  pinMode(rightIN1, OUTPUT);
  pinMode(rightIN2, OUTPUT);


void loop() {

  // Read the initial value of A4 (or whatever sharpIR is defined as):
  irVal = analogRead(sharpIR);

  // The Buggy will execute the code inside the 'while' brackets
  // as long as the IR sensor input isn't higher than the
  // 'tooClose' value

  // This original statement compared the pin number (0) to 'tooClose,'
  // always returning true. This causes the buggy to spin in circles
  // ad infinitum...
  // while(sharpIR<tooClose){
  // -------------------------------------------------------------------
  // Instead, we should compare the value read from A4 to 'tooClose.'
  // Also, the Sharp IR module specified in the bill of materials
  // reads higher as the distance closes, so we should be watching for
  // when the value on A4 is greater than, not less than, 'tooClose.'
  // This works:
  while(irVal > tooClose) {

    // The wheel direction pins are set according to the
    // HUB-ee datasheet and the speed output is set to
    // 200, pretty quick.
    digitalWrite(leftIN1, HIGH);
    digitalWrite(leftIN2, LOW);
    analogWrite(leftPWM, 200);

    // The wheel direction pins are set opposite on  
    // the right wheel because it needs to turn the
    // same direction as the left and its mirrored.
    digitalWrite(rightIN1, LOW);
    digitalWrite(rightIN2, HIGH);
    analogWrite(rightPWM, 200);

    // It is crucially important that you do not change the direction
    // of the HUB-ee wheels too rapidly. I learned this $55 lesson
    // the hard way.
    // Let's put in a delay, otherwise we're reading from
    // A4 as fast as this while loop can execute

    // Read the value of the Sharp IR sensor
    irVal = analogRead(sharpIR);


    // When the IR sensor detects a close object, the code
    // outside the brackets above will execute. In this
    // case, we're reversing one of the wheels to make
    // the Buggy turn. We'll run it this way for half a
    // second (500 milliseconds) before returning to
    // normal mode.
    digitalWrite(leftIN1, LOW);
    digitalWrite(leftIN2, HIGH);

Note: It's best to give the wheels a small delay before switching directions. Doing so too quickly may result in damaging the wheels.