dodany czujnik
Fork of HCSR04 by
Revision 1:0e7a98b41a75, committed 2016-06-17
- Comitter:
- jguzik
- Date:
- Fri Jun 17 08:54:31 2016 +0000
- Parent:
- 0:86b2086be101
- Commit message:
Changed in this revision
hcsr04.cpp | Show annotated file Show diff for this revision Revisions of this file |
hcsr04.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 86b2086be101 -r 0e7a98b41a75 hcsr04.cpp --- a/hcsr04.cpp Mon Apr 14 08:23:09 2014 +0000 +++ b/hcsr04.cpp Fri Jun 17 08:54:31 2016 +0000 @@ -3,7 +3,7 @@ /* *HCSR04.cpp */ -HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {} +HCSR04::HCSR04(PinName t, PinName e, long to) : trig(t), echo(e), time_out(to){} long HCSR04::echo_duration() { timer.reset(); //reset timer @@ -12,18 +12,26 @@ trig=1; // trigger high wait_us(10); trig=0; // trigger low - while(!echo); // start pulseIN - timer.start(); - while(echo); - timer.stop(); - return timer.read_us(); + 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(){ + long HCSR04::distance(){ duration = echo_duration(); - distance_cm = (duration/2)/29.1 ; - return distance_cm; + distance_cm = (duration*34)/1000/2 ; + return distance_cm; } \ No newline at end of file
diff -r 86b2086be101 -r 0e7a98b41a75 hcsr04.h --- a/hcsr04.h Mon Apr 14 08:23:09 2014 +0000 +++ b/hcsr04.h Fri Jun 17 08:54:31 2016 +0000 @@ -33,10 +33,9 @@ #include "mbed.h" - class HCSR04 { public: - HCSR04(PinName t, PinName e); + HCSR04(PinName t, PinName e, long to); long echo_duration(); long distance(); @@ -44,7 +43,7 @@ DigitalOut trig; DigitalIn echo; Timer timer; - long duration,distance_cm; + long duration,distance_cm,time_out; }; #endif \ No newline at end of file