driver for the ranger the 2 piezo version

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ranger.cpp Source File

Ranger.cpp

00001 #include "Ranger.h"
00002 
00003 
00004 
00005 // IO bits for the rangefinder
00006 DigitalOut trig(PTD7);
00007 DigitalIn echo(PTD6);
00008 // timer to handle rangefinder 
00009 Timer t;
00010 
00011 int range(){
00012     int rangecm=0,rtime=0;
00013     
00014     // reset thetimer to zero
00015       t.reset();
00016     
00017 
00018         //send a trigger pulse
00019         trig=1;
00020         wait_us(12);
00021         trig=0;
00022         wait_us(12);
00023         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
00024         // sets the actual trigger pin to low and low on this pin sets it to high - this is becausethe ranger needs 5V for high
00025         // wait for echo to go high
00026         while(echo==0);
00027         t.start();
00028         while(echo==1 && t.read_us()<58*300); // wait for echo to go low again or for about 3m of range
00029         t.stop();
00030         rtime=t.read_us();
00031         t.reset();
00032         rangecm=rtime/58; // convert time in us to range in cm
00033         return rangecm; // return range in cm
00034     
00035     
00036     }