2nd year embedded systems project

Dependencies:   mbed C12832

Door_1_Docs/hcsr04.cpp

Committer:
nwabiam
Date:
2020-12-15
Revision:
1:9d7c34bfe43e

File content as of revision 1:9d7c34bfe43e:

#include "hcsr04.h"
#include "mbed.h"
/*
*HCSR04.cpp
*/
HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
 float 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 
float HCSR04::distance(){
    duration = echo_duration();
  distance_cm = (duration/2)/29.1  ;
        return distance_cm;
 
}