Roaming Arduino Robot with Ping!

I have gotten some code together, mostly from scratch because it seemed that none of the examples i tried actually worked.

The code is pretty basic, but at least got it moving, without smoking wires or any silicon goodness spilling out of my h-bridge chips,  Which is always a special treat.  I will be continuing work on this bot, and posting future code and updates as they come avialble.

If you are using the arduino motor shield available from Adafruit then this code is perfect.  If you aren’t.. then it probably won’t work for ya.  Sorry.

Here’s the current code:

/*

OBJECT AVOIDING ROBOT CODE FOR ARDUINO – ADAFRUIT MOTORSHIELD – PARALLAX PING )) – TAMIYA DUAL MOTOR GEARBOX

CODE ADAPTED, COPIED, MODIFIED, AND STOLEN IN GENERAL FROM VARIOUS PLACES AROUND THE INTERNET BY GRINAN BARRETT JULY 4TH/5TH 2008

THIS CODE IS FREE TO ANYONE THAT WISHES TO USE IT, MODIFY IT, OR STEAL IT IN GENERAL.. HAVE FUN WITH IT!

This code will let you program a bot that will roam around looking for things to avoid running into.  The code is pretty simple, but gave me major fits figuring out.  Espeically the interaction with the
the parallax ping )) ultrasonic range finder.  The main thing to remember is the intialization of the ping unit.. it has to be exact.. once i saw i was running to tight a loop on it.. things got much easier.. ( thanks collin)
if nothing else, this code will let you have the basics of a roving bot with ping capability, it’s up to you to do cool stuff with it.

*/

//    GETTING EVERYTHING SETUP AND READY
#include <AFMotor.h>
AF_DCMotor motor(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
unsigned long echo = 0;
unsigned long ultrasoundValue = 0;
int ultraSoundSignal = 14;    // sets enable to pin 14
float dist = 0;      // variable to store the converted distance in inches

// ***************************   MAIN STRAIGHT ROUTINE  *********************************** \\
void straight()
{
motor.run(FORWARD);
motor2.run(FORWARD);
Serial.print(“Forward Hoooo!”);  // ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}
// ***************************   MAIN TURNING ROUTINE  *********************************** \\
void turnRight()
{
motor.run(BACKWARD);
motor2.run(FORWARD);
Serial.print(“Turning Right!!”);// ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}

// ***************************   PING FUNCTIONS TO GET DISTANCES  *********************************** \\
float distCalc() // distance calculating function converts analog input to inches
{
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor

echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
dist = (echo / 58.138) * .39; //convert to CM then to inches

return dist;

}

// ***************************   THE AVOIDING OBJECTS ROUTINE  *********************************** \\
void AvoidObjects()
{
if(dist < 8)                            // if the distance is less than 8 inches
{
Serial.print(“object detected closer than allowed!!!”);   // ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
turnRight();                            // turns right using turnRight function
delay(100);
}

else                                      // otherwise
{
straight();                            // go straight using the straight function
Serial.print(” continuing forward “);  //  ADDED SO I COULD SEE PROBLEMS WITH SERIAL MONITOR
}
}

// ***************************   SETTING UP THE PROGRAM  *********************************** \\
void setup()
{

motor.setSpeed(200);          // sets motor 1 speed to 200
motor2.setSpeed(200);          // sets motor 2 speed to 200

pinMode(ultraSoundSignal, OUTPUT);  // sets enable as output

straight();                    // initializes go straight
}

// ***************************   MAIN PROGRAM LOOPS  *********************************** \\
void loop() {
Serial.begin(9600);
distCalc();
AvoidObjects();
delay(20);
int x = 0;
x = dist;
Serial.println(x);  //     ADDED TO APPEND DISTANCE TO SERIAL MONITOR SO I COULD SEE PROBLEMS..
delay(250);
}

Have fun and let me know if it works for ya or you have problems with it!

Leave a comment