In response to the PM that the OP sent me, saying:
My ping sensor is located on pin number 7. I don't know how to address the sensor other than referring to the pin it is connected to. This is where my issue is. This is the only problem I have. This is why I am having trouble creating a proper function.
First, look on the playground - Arduino Playground - HomePage - which has a lot of information, including a new library for using ping sensors.
The ping sensors don't provide a output that can be read directly. They are called ping sensors because you send out a ping, and see how long it takes for the echo to come back, just like a sonar. That is what this code does:
long duration, inches;
// Set the pin for output
pinMode(pingPing, OUTPUT);
// Send out a ping
digitalWrite(pingPing, LOW);
delayMicroseconds(2);
digitalWrite(pingPing, HIGH);
delayMicroseconds(5);
digitalWrite(pingPing, LOW);
// Set the pin for input
pinMode(pingPing, INPUT);
// Wait for the echo to return, store the result in duration.
duration = pulseIn(pingPing, HIGH);
See http://arduino.cc/en/Reference/PulseIn for details on pulseIn().
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
This function takes the duration passed to it, and returns a number that represents the distance, in inches, between the sensor, and whatever the ping reflected back from.
AWOL, in the previous post, has provided a function that you can add to your sketch, to replace the inline ping code. If you add it, you can incorporate it into your robotRight() function, something like:
void robotRight()
{
if (microsecondsToInches(pingRange(pingPing)) < 10)
{
delay(500);
myservoA.write(90);
delay(2000);
}
else
{
myservoA.write(0);
}
}
Hopefully this will help you. If not, please stick to using the forum, as it can help others too.