Now working with floats instead of long ints
Fork of HCSR04 by
Embed:
(wiki syntax)
Show/hide line numbers
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 float 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 float HCSR04::distance(){ 00025 duration = echo_duration(); 00026 distance_cm = (duration/2)/29.1 ; 00027 return distance_cm; 00028 00029 }
Generated on Tue Aug 23 2022 16:56:47 by 1.7.2