Librería HCSR04 con algunas modificaciones
Diff: HCSR044.cpp
- Revision:
- 0:d4d01f7a6c68
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR044.cpp Thu Dec 04 06:28:23 2014 +0000 @@ -0,0 +1,41 @@ +#include "HCSR044.h" +#include "mbed.h" + + +HCSR044::HCSR044(PinName t, PinName e) : trig(t), echo(e) {} + +// Trigger Echo +// _______ _____________,,,,,,,,, +// ____| 10us |_________| 150us-25ms, or 38ms if no obstacle +// + +//return echo duration in us (refer to digram above) +long HCSR044::echo_duration() { + timer.reset(); + trig = 0; + wait_us(2); + trig = 1; + wait_us(10); + 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 HCSR044::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; +} +