Librería HCSR04 con algunas modificaciones

Dependents:   ZMOTO42

Committer:
jaruiz
Date:
Thu Dec 04 06:28:05 2014 +0000
Revision:
0:d019c9870689
Librer?a HCSR04 con algunas modificaciones

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jaruiz 0:d019c9870689 1 #include "HCSR043.h"
jaruiz 0:d019c9870689 2 #include "mbed.h"
jaruiz 0:d019c9870689 3
jaruiz 0:d019c9870689 4
jaruiz 0:d019c9870689 5 HCSR043::HCSR043(PinName t, PinName e) : trig(t), echo(e) {}
jaruiz 0:d019c9870689 6
jaruiz 0:d019c9870689 7 // Trigger Echo
jaruiz 0:d019c9870689 8 // _______ _____________,,,,,,,,,
jaruiz 0:d019c9870689 9 // ____| 10us |_________| 150us-25ms, or 38ms if no obstacle
jaruiz 0:d019c9870689 10 //
jaruiz 0:d019c9870689 11
jaruiz 0:d019c9870689 12 //return echo duration in us (refer to digram above)
jaruiz 0:d019c9870689 13 long HCSR043::echo_duration() {
jaruiz 0:d019c9870689 14 timer.reset();
jaruiz 0:d019c9870689 15 trig = 0;
jaruiz 0:d019c9870689 16 wait_us(2);
jaruiz 0:d019c9870689 17 trig = 1;
jaruiz 0:d019c9870689 18 wait_us(10);
jaruiz 0:d019c9870689 19 trig = 0;
jaruiz 0:d019c9870689 20 while(echo == 0);
jaruiz 0:d019c9870689 21 timer.start();
jaruiz 0:d019c9870689 22 while(echo == 1);
jaruiz 0:d019c9870689 23 timer.stop();
jaruiz 0:d019c9870689 24 return timer.read_us();
jaruiz 0:d019c9870689 25 }
jaruiz 0:d019c9870689 26
jaruiz 0:d019c9870689 27 //return distance to nearest obstacle or returns -1
jaruiz 0:d019c9870689 28 //if no obstacle within range
jaruiz 0:d019c9870689 29 //set sys to cm or inch accordingly
jaruiz 0:d019c9870689 30 long HCSR043::distance(int sys){
jaruiz 0:d019c9870689 31 duration = echo_duration();
jaruiz 0:d019c9870689 32 if(duration > 30000)
jaruiz 0:d019c9870689 33 return -1;
jaruiz 0:d019c9870689 34 distacne_cm = duration /29 / 2 ;
jaruiz 0:d019c9870689 35 distance_inc = duration / 74 / 2;
jaruiz 0:d019c9870689 36 if (sys)
jaruiz 0:d019c9870689 37 return distacne_cm;
jaruiz 0:d019c9870689 38 else
jaruiz 0:d019c9870689 39 return distance_inc;
jaruiz 0:d019c9870689 40 }
jaruiz 0:d019c9870689 41