Development and testing of ultrasonic distance measurement library for device HC-SR04.
Dependencies: TextLCD_improved mbed Distance_HC_SR04
Diff: main.cpp
- Branch:
- CLASS_IMPLEMENTATION
- Revision:
- 3:cb5931861f4e
- Parent:
- 2:aba8d0d53190
- Child:
- 4:bb5819be6ac3
--- a/main.cpp Sun Dec 20 21:24:45 2015 +0000 +++ b/main.cpp Sun Dec 20 21:37:27 2015 +0000 @@ -9,7 +9,7 @@ #define CALC_COEFF (1000.0f*340.0f/2) #define TICKS_RANGE_MAX (15000) #define TICKS_RANGE_MIN (150) -#define TRIG_PULSE_US (500) +#define TRIG_PULSE_US (50) typedef enum { IDLE, STARTED, COMPLETED, TIMEOUT, OUT_OF_RANGE_MIN, OUT_OF_RANGE_MAX, ERROR_SIG } Distance_HC_SR04_state; @@ -46,6 +46,8 @@ void trigger(void) { if (_state == IDLE && _echo == 0) { _timeout.detach(); + _echo.rise(NULL); + _echo.fall(NULL); _timer.stop(); _timer.reset(); @@ -112,10 +114,15 @@ while (_state == STARTED) ; + _echo.rise(NULL); + _echo.fall(NULL); + _timeout.detach(); + switch (_state) { case COMPLETED: break; default: + _ticks_us = 0; break; } @@ -146,7 +153,6 @@ private: DigitalOut _trig; InterruptIn _echo; - uint32_t _ticks_us; uint32_t _tout_us; float _coeff; @@ -154,6 +160,7 @@ Timeout _timeout; volatile Distance_HC_SR04_state _state; + uint32_t _ticks_us; };