Librería HCSR04 con algunas modificaciones

Dependents:   ZMOTO42

Files at this revision

API Documentation at this revision

Comitter:
jaruiz
Date:
Thu Dec 04 06:28:05 2014 +0000
Commit message:
Librer?a HCSR04 con algunas modificaciones

Changed in this revision

HCSR043.cpp Show annotated file Show diff for this revision Revisions of this file
HCSR043.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r d019c9870689 HCSR043.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR043.cpp	Thu Dec 04 06:28:05 2014 +0000
@@ -0,0 +1,41 @@
+#include "HCSR043.h"
+#include "mbed.h"
+
+
+HCSR043::HCSR043(PinName t, PinName e) : trig(t), echo(e) {}
+
+//      Trigger          Echo
+//      _______           _____________,,,,,,,,,
+// ____|  10us |_________| 150us-25ms, or 38ms if no obstacle
+// 
+
+//return echo duration in us (refer to digram above)
+long HCSR043::echo_duration() {
+    timer.reset();
+    trig = 0;
+    wait_us(2);
+    trig = 1;
+    wait_us(10);
+    trig = 0;
+    while(echo == 0);
+    timer.start();
+    while(echo == 1);
+    timer.stop();
+    return timer.read_us();
+}
+
+//return distance to nearest obstacle or returns -1 
+//if no obstacle within range
+//set sys to cm or inch accordingly
+long HCSR043::distance(int sys){
+    duration = echo_duration();
+    if(duration > 30000)
+        return -1;
+    distacne_cm = duration /29 / 2 ;
+    distance_inc = duration / 74 / 2;
+    if (sys)
+        return distacne_cm;
+    else
+        return distance_inc;
+}
+
diff -r 000000000000 -r d019c9870689 HCSR043.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR043.h	Thu Dec 04 06:28:05 2014 +0000
@@ -0,0 +1,39 @@
+//Library for controlling ultrasonic module HCSR04
+//Ported by hiawoood from arduino library orgininally created by ITead studio.
+//Instantiate object by supplying the proper pin numbers of "trigger" and "echo"
+//e.g. 
+/*
+        int main() {
+            Ultrasonic sensor(p5, p6);
+            while(1){
+                long distance = sensor.distance(CM);
+                printf("Distance:%d\n");
+                wait(0.1);
+            }
+        }
+*/
+
+
+
+#ifndef HCSR043_H
+#define HCSR043_H
+
+#include "mbed.h"
+
+#define CM 1
+#define INC 0
+
+class HCSR043 {
+  public:
+    HCSR043(PinName t, PinName e);
+    long echo_duration();
+    long distance(int sys);
+
+    private:
+        DigitalOut trig;
+        DigitalIn echo;
+        Timer timer;
+        long duration,distacne_cm,distance_inc;
+};
+
+#endif
\ No newline at end of file