![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Proof of concept for distance and temperature monitoring
Dependencies: mbed mbedConnectorInterface mbedEndpointNetwork
Diff: hcsr04.cpp
- Revision:
- 8:c69fe28366d8
- Parent:
- 7:939fdc8df95b
- Child:
- 10:338191178cbf
--- a/hcsr04.cpp Wed May 06 02:53:36 2015 +0000 +++ b/hcsr04.cpp Wed May 06 04:02:08 2015 +0000 @@ -38,11 +38,11 @@ void HCSR04::isr_rise(void) { pulsetime.start(); + pulsetime.reset(); } void HCSR04::start(void) { pulsedur = 0; - distance = 0; trigger=1; wait_us(10); @@ -53,8 +53,6 @@ { pulsetime.stop(); pulsedur = pulsetime.read_us(); - distance= (pulsedur*343)/20000; - pulsetime.reset(); } void HCSR04::rise (void (*fptr)(void)) @@ -66,9 +64,11 @@ echo.fall(fptr); } -unsigned int HCSR04::get_dist_cm() +double HCSR04::get_dist_cm() { - return distance; + if (pulsedur == 0) + return -1; + return pulsedur * kMicrosecondsToCentimeters; } unsigned int HCSR04::get_pulse_us() {