Pulse signal measurement trouble

28 Jul 2013

Hi all,

I have recently asked about setting up a program to measure the length of a pulse signal and was pointed in the direction of some other peoples codes they had put together. After looking through them and attempting to get them working I am still struggling. The sensor I have ( http://www.maxbotix.com/documents/MB7060-MB7070_Datasheet.pdf ) gives out a pulse according to distance measured (58us per cm ). I have imported a program and tried to get it going. I have changed the trigger signal form 10us to 25us as the data sheet stated a minimum of 20us so I went for 25 to be safe. The data sheet also states that pin 4 is used for the trigger signal but I don't know what output from the microcontroller would do this. I am unsure about setting up the pins for the sensor to the microcontroller. The way its named in the code makes it a bit confusing for me (sorry i'm a bit of a novice when it comes to this). Also the existing program is set up for a different type of sensor but I was informed it should operate ok for mine as it is just measuring pulse duration. This is what I have:

 #include "mbed.h"
#include "Terminal.h"
#include "HCSR04.h"
 
HCSR04::HCSR04(PinName 9, PinName 10) : trig(t), echo(e) {}

 
//      Trigger          Echo
//      _______           _____________,,,,,,,,,
// ____|  25us |_________| 150us-25ms, or 38ms if no obstacle
// 
 
//return echo duration in us (refer to digram above)
long HCSR04::echo_duration() {
    timer.reset();
    trig = 0;
    wait_us(2);
    trig = 1;
    wait_us(25);
    trig = 0;
    while(echo == 0);
    timer.start();
    while(echo == 1);
    timer.stop();
    return timer.read_us();
}
 
//return distance to nearest obstacle or returns -1 
//if no obstacle within range
//set sys to cm or inch accordingly
long HCSR04::distance(int sys){
    duration = echo_duration();
    if(duration > 30000)
        return -1;
    distacne_cm = duration /29 / 2 ;
    distance_inc = duration / 74 / 2;
    if (sys)
        return distacne_cm;
    else
        return distance_inc;
}
 
 
 int main() {
            U
            while(1){
                long distance = sensor.distance(CM);
                term.printf("Distance:%d\n");
                wait(0.1);
            }
        }>>  

If anyone could give me some help it would be greatly appreciated as I am currently pulling my hair out!

Many Thanks