Proof of concept for distance and temperature monitoring

Dependencies:   mbed mbedConnectorInterface mbedEndpointNetwork

Revision:
10:338191178cbf
Parent:
8:c69fe28366d8
--- a/hcsr04.cpp	Thu May 07 04:14:57 2015 +0000
+++ b/hcsr04.cpp	Mon May 18 00:44:59 2015 +0000
@@ -22,7 +22,7 @@
 
 
 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
-    trigger(TrigPin), echo(EchoPin)
+    trigger(TrigPin), echo(EchoPin), maxpulse(-1)
 {
     pulsetime.stop();
     pulsetime.reset();
@@ -42,7 +42,7 @@
 }
 void HCSR04::start(void)
 {
-    pulsedur = 0;
+    pulsedur = -1;
     
     trigger=1;
     wait_us(10);
@@ -53,6 +53,8 @@
 {
     pulsetime.stop();
     pulsedur = pulsetime.read_us();
+    if (pulsedur > maxpulse && pulsedur < kBogusPulse)
+        maxpulse = pulsedur;
 }
 
 void HCSR04::rise (void (*fptr)(void))
@@ -66,11 +68,13 @@
 
 double HCSR04::get_dist_cm()
 {
-    if (pulsedur == 0)
-        return -1;
     return pulsedur * kMicrosecondsToCentimeters;
 }
-unsigned int HCSR04::get_pulse_us()
+double HCSR04::get_max_dist_cm()
+{
+    return maxpulse * kMicrosecondsToCentimeters;
+}
+int HCSR04::get_pulse_us()
 {
     return pulsedur;
 }