In "pingsensor" you have a variable called "duration", but it is local, so is unavailable to the rest of the sketch.In fact, it seems that "pingsensor" serves no purpose other than reporting a range to the serial monitor.
Isn't the duration the value the says how close the wall the robot is?
if (pulseDuration < 10 & pulseDuration > 5)
Have you abandoned the thought of following the suggestions in replies 7 and 9?
Please enter a valid email to subscribe
We need to confirm your email address.
To complete the subscription, please click the link in the
email we just sent you.
Thank you for subscribing!
via Egeo 16