I would like this code to take the inputs from two stationary ping sonar sensors and steer a car to follow another car in front of it. The car has two continuous rotation servos to move the car and one pan servo to steer the car. When the code runs, the serial monitor shows the sensor values and the continuous rotation servos operate correctly. When an object is placed several feet in front of the sensors the servos stop moving and the sensors stop pinging. After a period of time, the sensors will start pinging and the servos will start turning. can someone help determine why this is happening instead of the sensors and servos operating continuously to steer thee car as desired.

else if ((leftDistance > rightDistance) && (leftDistance > dangerThresh)) //if target distance is greater than programmed default AND if target is closer on the RIGHT side...turn RIGHT to face target {

turnRight(); }

else if ((rightDistance > leftDistance) && (rightDistance > dangerThresh)) //if target distance is greater than programmed default AND if target is closer on the LEFT side...turn LEFT to face target {

Thanks for your replies, here is what i have done and what i have found. I removed the part of code in setup that set the variables to zero, I commented out the if statement at the end that stopped the servos and ran the program with no objects within four feet of the ping sensors. Below are the following serial monitor results for about fifteen seconds. The results that are zero are the result of the ping sensors not pinging (light did not flash on either sensor). The servos are powered by a separate 4AA battery pack.

That shows that pingLeft() and pingRight() are sometimes producing an answer of zero.

I suggest you write a simplified sketch that only calls one of those functions at regular intervals and prints the results, and test it until you are sure it produces reliable answers under all conditions.

I can't see from your output - can you confirm that the sketch does actually keep running at the same speed and it only the ping results which are going wrong?

I see that you are using delayMicroseconds() with quite small values to trigger the ping. Where did those values come from? The online reference says this about delayMicroseconds():

Quote

This function works very accurately in the range 3 microseconds and up. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times.

I only provide help via the forum - please do not contact me for private consultancy.

Here is my updated code. This code currently pings each sensor every three ms while activating the continuous motion servos until either one or both sensors detects an object within the dangerThresh which is currently set at ten cm. Once the object is greater than dangerThresh, the continuous rotation servos will start up again. so now my problem seems to be that the pan servo does not turn right or left as directed by the if statements for Right and Left respectively. Thanks for your help so far.

else if ((leftDistance > rightDistance) && (rightDistance > dangerThresh)) //if target distance is greater than programmed default AND if target is closer on the RIGHT side...turn RIGHT to face target {

turnRight(); }

else if ((rightDistance > leftDistance) && (leftDistance > dangerThresh)) //if target distance is greater than programmed default AND if target is closer on the LEFT side...turn LEFT to face target {

void loop(){ pingRight(); rightDistance = microsecondsToCentimeters(durationRight); delay(300); pingLeft(); leftDistance = microsecondsToCentimeters(durationLeft); delay(300);Presumably the pingRight and pingLeft functions are meant to measure the distances right and left, but if you look at either of them they do not move the pan servo.

Please do not send me PMs asking for help. Post in the forum then everyone will benefit from seeing the questions and answers.

Suggest you put a print statement in each of those 'if' blocks so that you can tell what it thinks it is doing - also add a final 'else' to tell you when none of those conditions are true. Since you already print out the values used in those conditions, it should now be obvious what it is doing and why.

I only provide help via the forum - please do not contact me for private consultancy.