LOL...that does sound fun, though it's programmed for object avoidance right now. I haven't figured out how to do motion following yet...I'd eventually like to.
Right now the robot will pivot clockwise within 24" of any object. So if the better route of travel is the left, it will never make it.
I figure if I have the head scan side to side at about 90 degrees, I can somehow program it to head in the direction with most available distance. I don't have the logic for the code quite sorted out in my head yet so if anyone has any advice, I welcome it!
Here's my current code if that is helpful to anyone else:
const int rightPIN=5;
const int leftPIN=3;
const int pingPin = 4;
const int avoidinch = 22;
int leftforward=42; //was 290
int rightforward=170;
int leftbackward=80;
int rightbackward=80;
void setup()
{
Serial.begin (9600);
pinMode (leftPIN,OUTPUT);
pinMode (rightPIN,OUTPUT);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches;
// 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(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
if (inches>avoidinch)
{ _forward(); }
else
{ _clockwise(); }
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
void _forward()
{
analogWrite (leftPIN,leftforward);
analogWrite (rightPIN,rightforward);
}
void _clockwise() //confirmed
{
analogWrite (leftPIN,leftforward);
analogWrite (rightPIN,rightbackward);
}