Help with Programming Sonar

Hello everyone,
I have built a robot that will drive down a hallway and when it gets close enough to the wall the sonar sensor will detect it and the wheel that is in the middle will move it into the middle again and repeat. The only problem is i do not know how to program it. If some one could share some programming or ideas of how that would be great.

Thanks in advance,
Nick

For the sake of learning, I would advise you experiment for yourself and figure it out.

Assuming you’re using RobotC:
Start by declaring all your motors and sensors in the Motor and Sensor Setup. Then create a basic driver control program that allows you to drive the robot. Use the debugger to see the values your sonar sensor returns as you drive too close to walls. Then, in your code, incorporate that logic:

if(SensorValue[sonarSensor] < minimum){
  //Corrective maneuver
}
else if(SensorValue[sonarSensor] > maximum){
  //Opposite corrective maneuver
}
else {
  //No corrective maneuver
}

The Vex Inventor’s Guide has information on the Ultrasonic Ranger Sensors. You can also browse the programming examples that come with either Robot C or Easy C Pro.

Tech Support responded with this recommendation “My recommendation will be to instead test your sensor with the “Ultrasonic Test” program that easyC provides under sample programs. All you have to do is click on: File> Open Project> Samples> Ultrasonic Test.” If you are using Easy C or Easy C for Cortex it should help you.