Let's Make Robots!

PING sensor question

Oh hello there! I'm pretty new with robotics and all of that exciting stuff. I'd been wanting a microcontroller for a few years, and when mbed gave out free mbeds, (yay) I got one and messed around with it a bit.

I got a Parallax PING sensor a couple weeks ago, and have finally gotten around to trying to use it. Here's my problem: The sensor blinks when I send it a signal, but it doesn't send anything back. The sensor is rated for 5 volts, and I've gotten it to work with both 4.5v and 6v. (6v was only for testing, I'm not planning on using it with 6v all the time.)

My exact C++ code is the following: (hopefully you'll be able to make out what it does, it's pretty heavily commented. It's code copied straight from the mbed site. I did make a few debugging changes, but those shouldn't affect it too much.)

#include "mbed.h"

DigitalInOut pingPin(p10); //sets ping pin to pin 10 (digital in and out)
DigitalOut ld1(LED1); //sets "ld1" as the built-in LED1
DigitalOut ld2(LED2);

Timer tmr; //creates a timer

long microsecondsToInches(long microseconds);
long microsecondsToCentimeters(long microseconds);

int main() {
while (1) {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pingPin.output();//set ping pin to output
pingPin = 0;
wait_us(2);
pingPin = 1;
wait_us(5);
pingPin = 0;
ld1=1;

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pingPin.input();
while (!pingPin); // wait for high
tmr.start();
while (pingPin); // wait for low
duration = tmr.read_us();
ld2=1;

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
wait_ms(100);
}
}

long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

 

 

I appreciate the help, and if I haven't been very clear, just ask for more details.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I did originally have some code to display the distance to the user, but I got rid of it in testing. I turn LED2 on when I recieve the signal, but LED2 never turns on. I presume that means there's no signal coming back to the controller, meaning it never does get to the code to write the inches/cm variable anyway.

Your code looks good but is that all of it? It probably does exactly what it is supposed to do and stores the distance numbers in the variables "inches" and "cm". You just need to ask yourself how do you want to display the distance? There seems to be no code here to interface with a user.

For example many people use a serial port and terminal program so the micro can talk to your computer and write out the distance. You could also use an LCD display, a buzzer that changes pitch depending on distance or an LED that changes brightness.