Library to use the ultrasonic sensor

Dependents:   test_ultrasonic AEB Car_Simulator

Revision:
1:b3518845e71a
Parent:
0:72ec72845f71
Child:
2:12da800fd10a
diff -r 72ec72845f71 -r b3518845e71a Ultrasonic.cpp
--- a/Ultrasonic.cpp	Thu Jun 02 19:56:38 2016 +0000
+++ b/Ultrasonic.cpp	Sat Jun 04 18:13:44 2016 +0000
@@ -7,6 +7,7 @@
 InterruptIn echo(ECHO);       // Attach interrupt to the echo pin
 Timer timer;
 Ticker tick;
+Timeout timeout;
 
 /*
 * Call this function and it will return the distance in centimeter
@@ -28,6 +29,7 @@
     timer.stop();
     //pc.printf("Distance: %f \n", DistanceCM);
     timer.reset();
+    timeout.detach();
 }
 
 void trig()
@@ -35,6 +37,7 @@
     trigger = 1;
     wait_us(10);
     trigger = 0;
+    timeout.attach(&timeout_err, 0.05);
 }
 
 
@@ -44,4 +47,12 @@
     echo.rise(&start);
     echo.fall(&stop);
     tick.attach(&trig,0.06);
+}
+
+void timeout_err()
+{
+    // Timeout elapsed -> there is some problem
+    // out the distance to zero to highligth error
+    
+    DistanceCM = 0;
 }
\ No newline at end of file