driver for the ranger the 2 piezo version

Files at this revision

API Documentation at this revision

Comitter:
cstevens
Date:
Tue Jun 14 13:33:39 2016 +0000
Commit message:
poster for the oxford CWM code

Changed in this revision

Ranger.cpp Show annotated file Show diff for this revision Revisions of this file
Ranger.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 5871422998cd Ranger.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ranger.cpp	Tue Jun 14 13:33:39 2016 +0000
@@ -0,0 +1,36 @@
+#include "Ranger.h"
+
+
+
+// IO bits for the rangefinder
+DigitalOut trig(PTD7);
+DigitalIn echo(PTD6);
+// timer to handle rangefinder 
+Timer t;
+
+int range(){
+    int rangecm=0,rtime=0;
+    
+    // reset thetimer to zero
+      t.reset();
+    
+
+        //send a trigger pulse
+        trig=1;
+        wait_us(12);
+        trig=0;
+        wait_us(12);
+        trig=1; // thing starts on trigger going high NB - this is buffered with an npn and is connected to its base. high on this pin
+        // sets the actual trigger pin to low and low on this pin sets it to high - this is becausethe ranger needs 5V for high
+        // wait for echo to go high
+        while(echo==0);
+        t.start();
+        while(echo==1 && t.read_us()<58*300); // wait for echo to go low again or for about 3m of range
+        t.stop();
+        rtime=t.read_us();
+        t.reset();
+        rangecm=rtime/58; // convert time in us to range in cm
+        return rangecm; // return range in cm
+    
+    
+    }
\ No newline at end of file
diff -r 000000000000 -r 5871422998cd Ranger.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ranger.h	Tue Jun 14 13:33:39 2016 +0000
@@ -0,0 +1,9 @@
+// Ranger.h file
+
+#include "mbed.h"
+// if RANGER_H has not previously been defined
+// define it now
+
+
+int range(); // ranger only has 1 function prototype
+