Library for Ultrasonic Range,
Fork of HCSR04 by
Revision 1:0b3bdb910fe7, committed 2016-06-21
- Comitter:
- yruiewyrui3
- Date:
- Tue Jun 21 07:45:27 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 |
diff -r 86b2086be101 -r 0b3bdb910fe7 hcsr04.cpp --- a/hcsr04.cpp Mon Apr 14 08:23:09 2014 +0000 +++ b/hcsr04.cpp Tue Jun 21 07:45:27 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,25 @@ 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(){ duration = echo_duration(); - distance_cm = (duration/2)/29.1 ; + distance_cm = (duration/2.00)/29.10 ; return distance_cm; } \ No newline at end of file