Ejemplo para entender funcionamiento del sensor Ultrasonico HCSR04

Dependencies:   mbed HCSR04

Revision:
2:287be83ed6a0
Parent:
1:4a5586eb1765
Child:
3:3e528b02b8d7
--- a/main.cpp	Thu Dec 04 08:04:55 2014 +0000
+++ b/main.cpp	Tue Feb 04 19:12:35 2020 +0000
@@ -1,23 +1,33 @@
 #include "mbed.h"
-#include "ultrasonic.h"
+#include "HCSR04.h"     //https://os.mbed.com/users/aralshukaili/code/HCSR04/
+
+HCSR04 sensor(PB_6, PB_7);
 
- void dist(int distance)
-{
-    //put code here to happen when the distance is changed
-    printf("Distance changed to %dmm\r\n", distance);
-}
+DigitalOut GREEN(PD_12);
+DigitalOut ORANGE(PD_13);
 
-ultrasonic mu(D8, D9, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
-                                        //have updates every .1 seconds and a timeout after 1
-                                        //second, and call dist when the distance changes
+
 
 int main()
 {
-    mu.startUpdates();//start mesuring the distance
-    while(1)
-    {
-        //Do something else here
-        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
-                                //the class checks if dist needs to be called.
+    while(1) {
+        long d = sensor.distance(1);
+        printf("Distancia:%d ",sensor.distance(1));
+
+        if(d<=10) {
+            GREEN = 0;
+            ORANGE = 1;
+        }
+
+        if(d>=200) {
+            ORANGE = 0;
+            GREEN = 1;
+        }
+        if(d>=11 && d<=39) {
+            ORANGE = 0;
+            GREEN=0;
+        }
+
+
     }
 }