Test

Files at this revision

API Documentation at this revision

Comitter:
fermedicius
Date:
Mon Jan 11 10:59:03 2021 +0000
Commit message:
Test

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 000000000000 -r 2a1510763167 hcsr04.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.cpp	Mon Jan 11 10:59:03 2021 +0000
@@ -0,0 +1,29 @@
+#include "hcsr04.h"
+#include "mbed.h"
+/*
+*HCSR04.cpp
+*/
+HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
+ long HCSR04::echo_duration() {
+        
+    timer.reset();  //reset timer
+    trig=0;   // trigger low 
+    wait_us(2); //  wait 
+    trig=1;   //  trigger high
+    wait_us(10);
+    trig=0;  // trigger low
+         while(!echo); // start pulseIN
+      timer.start();
+     while(echo);
+      timer.stop();
+     return timer.read_us(); 
+ 
+}
+ 
+//return distance in cm 
+long HCSR04::distance(){
+    duration = echo_duration();
+  distance_cm = (duration/2)/29.1  ;
+        return distance_cm;
+
+}
\ No newline at end of file
diff -r 000000000000 -r 2a1510763167 hcsr04.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.h	Mon Jan 11 10:59:03 2021 +0000
@@ -0,0 +1,20 @@
+#ifndef HCSR04_H
+#define HCSR04_H
+#include "mbed.h"
+
+
+ 
+class HCSR04 {
+  public:
+    HCSR04(PinName t, PinName e);
+    long echo_duration();
+    long distance();
+ 
+    protected:
+        DigitalOut trig;
+        DigitalIn echo;
+        Timer timer;
+        long duration,distance_cm;
+};
+ 
+#endif
\ No newline at end of file