driver for the ranger the 2 piezo version

Ranger.cpp

Committer:
cstevens
Date:
2016-06-14
Revision:
0:5871422998cd

File content as of revision 0:5871422998cd:

#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
    
    
    }