driver for the ranger the 2 piezo version
Diff: Ranger.cpp
- Revision:
- 0:5871422998cd
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