Library for Ultrasonic Range,

Dependents:   CZOSINHO

Fork of HCSR04 by Antoniolinux B.

hcsr04.cpp

Committer:
yruiewyrui3
Date:
2016-06-21
Revision:
1:0b3bdb910fe7
Parent:
0:86b2086be101

File content as of revision 1:0b3bdb910fe7:

#include "hcsr04.h"
#include "mbed.h"
/*
*HCSR04.cpp
*/
HCSR04::HCSR04(PinName t, PinName e, long to) : trig(t), echo(e), time_out(to){}
 long HCSR04::echo_duration() {
        
    timer.reset();  //reset timer
    trig=0;   // trigger low 
    wait_us(2); //  wait 
    trig=1;   //  trigger high
    wait_us(10);
    trig=0;  // trigger low
        while(!echo); // start pulseIN
    timer.start();
    while(echo)
    {
        if(timer.read_us()>time_out)        
            break;
    }
    timer.stop();
    long czas=timer.read_us();
    if(czas<300)
        return 0;
    return timer.read_us(); 
 
}
 
//return distance in cm 
long HCSR04::distance(){
    duration = echo_duration();
  distance_cm = (duration/2.00)/29.10  ;
        return distance_cm;

}