ultrasonic sensors

Hi, can anyone help me please. I am using Easy C V4. Can anybody tell me how I can write a program to hone in on a target using the ultrasonic sensor? I need help getting started.:confused:

For example: lining up on a stack of rings or a post. I appreciate any help!

What have you tried so far?
Can you get a distance value from the ultrasound?
If you print out the value from the ultrasound, and move your robot by hand, can the ultrasound even see a goal? a post? a stack of rings?
That is the first step.

If you mount your Ultrasound on a servo so it can rotate back and forth, like a radar dish, then you can have one section of the program rotate it back and forth to find the shortest distance, and another section of the program move the robot to rotate under the servo to center the servo value, so that when you drive forward, you’ll be aiming at the post.
Without the servo, then the program is more like a line follower, rotate robot to follow the shortest distance response.

you would probably need two ultrasonics that toe inwards. one will work fine to stop at the stack. i can write you a basic program if you would like

DevinC,That would be great. I would appreciate it very much if you could write a basic program for me. I am using Easy C V4. Thanks so much for your time and interest in helping me get started! :slight_smile:

ok well im at home and i dont have EasyC but i will type one out thats relatively easy to read.

unsigned int ultra ----- ultrasonic sensor (no value) 1,11 analog

startultrasonic 1,11
getultrasonic 1,11,ultra
{ while ( ultra > 5 )
getultrasonic 1,11,ultra
//getultrasonic data
// move forward
setmotor1 127
setmotor2 -127
{ if ( ultra == 5 )
getultrasonic 1,11,ultrasonic
// get ultrasonic data
// stop
setmotor1 0
setmotor2 0

that should be the basic program for you. if its hard to read i can send you the program when i get back to class on monday morning. and nobody bag on my code, its a hybrid between EasyC and C++. i indented it but its not showing up right so…

I would suggest making that line “if ( ultra < 6 )” just in case the robot is moving fast and you go past 5…