Library for controlling ultrasonic ranging module HCSR04 Ported by hiawoood from arduino library orgininally created by ITead studio.

Dependents:   mbed-os-example-FinalReal mbed-os-example-FinalReal

Files at this revision

API Documentation at this revision

Comitter:
eunmango
Date:
Sun Jun 16 04:37:08 2019 +0000
Parent:
0:0bda99bb39a4
Commit message:
dd

Changed in this revision

HCSR04.cpp Show annotated file Show diff for this revision Revisions of this file
HCSR04.h Show annotated file Show diff for this revision Revisions of this file
diff -r 0bda99bb39a4 -r b3401c36a10b HCSR04.cpp
--- a/HCSR04.cpp	Tue Oct 18 14:32:12 2011 +0000
+++ b/HCSR04.cpp	Sun Jun 16 04:37:08 2019 +0000
@@ -1,19 +1,12 @@
 #include "HCSR04.h"
 #include "mbed.h"
 
-
-HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
+Ultrasonic::Ultrasonic(PinName t, PinName e) : trig(t), echo(e) {
+        trig=0;
+}
 
-//      Trigger          Echo
-//      _______           _____________,,,,,,,,,
-// ____|  10us |_________| 150us-25ms, or 38ms if no obstacle
-// 
-
-//return echo duration in us (refer to digram above)
-long HCSR04::echo_duration() {
+int Ultrasonic::echo_duration() {
     timer.reset();
-    trig = 0;
-    wait_us(2);
     trig = 1;
     wait_us(10);
     trig = 0;
@@ -24,18 +17,8 @@
     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 HCSR04::distance(int sys){
+int Ultrasonic::distance(){
     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;
-}
-
+    _distance= duration /58 / 2 ;
+        return _distance;
+}
\ No newline at end of file
diff -r 0bda99bb39a4 -r b3401c36a10b HCSR04.h
--- a/HCSR04.h	Tue Oct 18 14:32:12 2011 +0000
+++ b/HCSR04.h	Sun Jun 16 04:37:08 2019 +0000
@@ -1,39 +1,20 @@
-//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 HCSR04_H
-#define HCSR04_H
+#ifndef Ultrasonic_H
+#define Ultrasonic_H
 
 #include "mbed.h"
 
-#define CM 1
-#define INC 0
-
-class HCSR04 {
+class Ultrasonic {
   public:
-    HCSR04(PinName t, PinName e);
-    long echo_duration();
-    long distance(int sys);
+    Ultrasonic(PinName t, PinName e);
+    int echo_duration();
+    int distance();
+    int interval;
 
     private:
         DigitalOut trig;
         DigitalIn echo;
         Timer timer;
-        long duration,distacne_cm,distance_inc;
+        int duration,_distance;
 };
 
 #endif
\ No newline at end of file