well I have got mine working nice.. it is a ping pan roam robot. uses an ultrasonic prox sensor "parallax" mounted to a servo that pans it left and right. during a late night search for some similar arduino code I hit the jackpot. I don't know who originally created it as I have omitted and rewritten notes along with the header showing that info, thanks whoever you are. "oops". here it is
#include <Servo.h> //include Servo library
const int RForward = 65; //speed value of drive servo, value 0 for full speed rotation
const int RBackward = 130; //speed value of drive servo, value 180 for full speed rotation
const int LForward = RBackward; //the servos must rotate in opposite directions in order to drive in the same direction
const int LBackward = RForward; //the servos must rotate in opposite directions in order to drive in the same direction
const int RNeutral = 95; //value 90 for center, my servo is a little off
const int LNeutral = 95; //value 90 for center, my servo is a little off
const int pingPin = 7; //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
const int irPin = 0; //Sharp infrared sensor connected to this arduino pin "not currently used"
const int dangerThresh = 20; //threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance; //distances on either side
Servo panMotor; //servo that pans the ping sensor
Servo leftMotor; //continuous rotation servo that drives the left wheel
Servo rightMotor; //continuous rotation servo that drives the right wheel
long duration; //time it takes to recieve PING))) signal
void setup()
{
rightMotor.attach(11); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
leftMotor.attach(10); //signal wire on left continuous rotation "drive servo connected to this arduino pin
panMotor.attach(6); //signal wire on pan servo for ping sensor connected to this arduino pin
panMotor.write(90); //set PING))) pan to center "90 by default"
}
void loop()
{
int distanceFwd = ping(); //ping stright ahead
if (distanceFwd>dangerThresh) //if the path is clear
{
leftMotor.write(LForward); //move forward
rightMotor.write(RForward); //move forward
}
else //if path is blocked
{
leftMotor.write(LNeutral); //stop left drive motor
rightMotor.write(RNeutral); //stop right drive motor
panMotor.write(0); //turn ping sensor right, 0 for full right
delay(500); //wait this many milliseconds
rightDistance = ping(); //ping
delay(500); //wait this many milliseconds
panMotor.write(170); //turn ping sensor left, 180 for full right "my servo hits stop limit and binds at 180"
delay(500); //wait this many milliseconds
leftDistance = ping(); //ping
delay(500); //wait this many milliseconds
panMotor.write(83); //return to center, "90 by default" my servo is off center
delay(100); //wait this many milliseconds
compareDistance(); //compare the two distances measured
}
}
void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
leftMotor.write(LBackward); //rotate left drive servo bacward "skid steer"
rightMotor.write(RForward); //rotate right drive servo forward "skid steer"
delay(1000); //run motors this many milliseconds
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward); //rotate left drive servo forward "skid steer"
rightMotor.write(RBackward); //rotate right drive servo backward "skid steer"
delay(1000); //run motors this many milliseconds
}
else //if they are equally obstructed
{
leftMotor.write(LForward); //rotate left drive servo forward
rightMotor.write(RBackward); //rotate right drive servo backward
delay(2000); //run motors this many milliseconds
}
}
Moderator edit:
tags added