Thanks PaulS. I am able to use your suggestion for the if statements.
I was able to figure out how to perform a rapid ping and sweep to increase the chances of detecting the target while the robot is in motion. What I need help with is code to compare the pos (sweep servo position) distance from one sweep position to the next and actually focus on maintaining track of the closest target. I don't need to continue the sweep once the target is acquired so want to have the ping sensor always track the target. This will help drive the robot to the target correcting its direction on the fly. The chkDistanceFwd variable after the ping() contains the ping() distance for the current ping() sensor direction. I need to be able to compare this distance (old distance) to the distance after the next ping() (new distance) and so on until the sweep has completed one complete cycle. After the cycle is complete, I want to point the ping sensor at the closest target and have the wheel servos turn the robot to face the target and drive forward.
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
panMotor.write(pos); // tell servo to go to position in variable 'pos'
ping();
chkDistanceFwd = microsecondsToCentimeters(duration); //ping stright ahead
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos >= 1; pos-=1) // goes from 180 degrees to 0 degrees
{
panMotor.write(pos); // tell servo to go to position in variable 'pos'
ping();
chkDistanceFwd = microsecondsToCentimeters(duration); //ping stright ahead
delay(15); // waits 15ms for the servo to reach the position
}