PING Ultrasonic Range Finder

03 Jan 2010

Has anyone made any attempt to get a PING from Parallax to work with an mbed?  I just gave it a shot this evening and failed :-(

I have it working on my Arduino using the example code here: http://arduino.cc/en/Tutorial/Ping

I tried looking at the SRF05 example code, and I tried to use it as much as I could, however PING uses the same pin for trigger and echo.  When I compiled my code I tried setting an InterruptIn on the same pin I was using for my DigitalOut trigger pin.  Then I set functions to run on the rising and falling edges.  However these interrupts never get called.

Any suggestions?

 

Matthew

03 Jan 2010

Hi Matthew,

Looking at the code you point to, a direct translation looks something like:

 

#include "mbed.h"

DigitalInOut pingPin(p5);
Timer t;

int main() {
    pingPin.output();
    wait_us(2);
    pingPin = 1;
    wait_us(5);
    pingPin = 0;
    pingPin.input();
    
    // pulseIn
    t.start();
    while(!pingPin); // wait for high
    int duration = t.read_us();
}

The function pulseIn just waits for the pin to go high, and returns the time, so I just used a timer.

You can't have two objects tied to the same digital pin at the same time, but you could dynamically create them (new/delete) to switch, or just wire up to two pins. Also, if you have some pseudo code of how you'd like it to work we could consider some API updates.

 

Simon

03 Jan 2010

Simon, thanks for the reply!  I am not sure why I couldn't see replicate the pulseIn function, and even more unsure why I didn't go look at the Arduino page to see how pulseIn worked!!

After reading your reply I took what I had already, and tweaked what you had for pulseIn here to match what the pulseIn function is supposed to do.  It says it waits for the pin to go high, then starts the timer, and as soon as the pin goes low it stops the timer and returns the microseconds the pin was high.

Anyway, thanks!  I have posted my working code here PingRangeFinder

 

Matthew

12 Mar 2011

Matthew,

Don't know if you are still working on this.

SRF05 library works with a slight mod.

There is Ping library as well.

Joel