You are correct... I did not look up == but after a bit of thinking, I really do not need it. Since the right and left pings (one ping function called 3 times as recommended) will probably rarely be equal, it makes more sense to compare them to the forward ping distance and should allow for more dynamic directional control. Also, when checking distance to either side of center on a flat surface, the side distance would actually be greater due to the change in sensor angle from center. So as long as we are farther away than 40cm in this case, and right and left distances are equal to or greater than the center distance, the robot should move forward.
if ((chkDistanceFwd > dangerThresh) && (chkDistanceFwd <= rightDistance) && (chkDistanceFwd <= leftDistance))
This next line compares the right distance to the forward distance and the left distance to the right distance and if either indicates that the right distance is shorter, the robot should turn right.
else if ((chkDistanceFwd > dangerThresh) && (chkDistanceFwd > rightDistance) || (chkDistanceFwd > dangerThresh) && (leftDistance > rightDistance))
Next is same basic code to turn left if the distance is shorter.
else if ((chkDistanceFwd > dangerThresh) && (chkDistanceFwd > leftDistance) || (chkDistanceFwd > dangerThresh) && (rightDistance > leftDistance))
This works after a fashion but it seems the only way I can control the turning is to do a slow short duration turn then stop the wheels while ping rechecks the distances.
void turnLeft() //if left is less obstructed
{
rightMotor.writeMicroseconds(RForwardSlow); //rotate right drive servo forward "skid steer"
leftMotor.writeMicroseconds(LBackwardSlow); //rotate left drive servo backward "skid steer"
delay(100); //run motors this many milliseconds
bothWheelStop();
}
This makes for slow, jerky progress towards the target. If I take out the delay, the motor drives too long and the robot is out of position and ends up tracking objects other than the target. Ping() portion of the loop() seems to have adequate delays to allow for moving the servo and processing the ping data, but what is the smallest effective value I can use to move the servo 20 degrees to the right of center then move it 40 degrees so it is 20 degrees to the left of center and still get accurate ping() values? I was thinking if I can move the servo back and forth faster and still get accurate distances, I could remove the delays in the right and left steering functions and the robot would move forward and autocorrect its direction as it moved without stopping its forward motion.
ping();
chkDistanceFwd = microsecondsToCentimeters(duration); //ping straight ahead
panMotor.write(70); //turn ping sensor right, 0 for full right
delay(300); //wait this many milliseconds
ping();
rightDistance = microsecondsToCentimeters(duration);
panMotor.write(110); //turn ping sensor left, 180 for full right
delay(300); //wait this many milliseconds
ping();
leftDistance = microsecondsToCentimeters(duration);
panMotor.write(pos); //return to center, "90 by default"
delay(100); //wait this many milliseconds
}