Library for Ultrasonic Range,

Dependents:   burobo_reciverV2 Nucleo_Ultrasonic-HCSR4 Test_UltraSonic Ultrasonic2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hcsr04.cpp Source File

hcsr04.cpp

00001 #include "hcsr04.h"
00002 #include "mbed.h"
00003 /*
00004 *HCSR04.cpp
00005 */
00006 HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
00007  long HCSR04::echo_duration() {
00008         
00009     timer.reset();  //reset timer
00010     trig=0;   // trigger low 
00011     wait_us(2); //  wait 
00012     trig=1;   //  trigger high
00013     wait_us(10);
00014     trig=0;  // trigger low
00015          while(!echo); // start pulseIN
00016       timer.start();
00017      while(echo);
00018       timer.stop();
00019      return timer.read_us(); 
00020  
00021 }
00022  
00023 //return distance in cm 
00024 long HCSR04::distance(){
00025     duration = echo_duration();
00026   distance_cm = (duration/2)/29.1  ;
00027         return distance_cm;
00028 
00029 }