Bike Light Back Side

Dependencies:   mbed

Committer:
kornvarrel
Date:
Wed Dec 09 10:15:06 2015 +0000
Revision:
0:b5bafad435c8
Bike Light Back Side

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kornvarrel 0:b5bafad435c8 1 #include "hcsr04.h"
kornvarrel 0:b5bafad435c8 2 #include "mbed.h"
kornvarrel 0:b5bafad435c8 3 /*
kornvarrel 0:b5bafad435c8 4 *HCSR04.cpp
kornvarrel 0:b5bafad435c8 5 */
kornvarrel 0:b5bafad435c8 6 HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
kornvarrel 0:b5bafad435c8 7 long HCSR04::echo_duration() {
kornvarrel 0:b5bafad435c8 8
kornvarrel 0:b5bafad435c8 9 timer.reset(); //reset timer
kornvarrel 0:b5bafad435c8 10 trig=0; // trigger low
kornvarrel 0:b5bafad435c8 11 wait_us(2); // wait
kornvarrel 0:b5bafad435c8 12 trig=1; // trigger high
kornvarrel 0:b5bafad435c8 13 wait_us(10);
kornvarrel 0:b5bafad435c8 14 trig=0; // trigger low
kornvarrel 0:b5bafad435c8 15 while(!echo); // start pulseIN
kornvarrel 0:b5bafad435c8 16 timer.start();
kornvarrel 0:b5bafad435c8 17 while(echo);
kornvarrel 0:b5bafad435c8 18 timer.stop();
kornvarrel 0:b5bafad435c8 19 return timer.read_us();
kornvarrel 0:b5bafad435c8 20
kornvarrel 0:b5bafad435c8 21 }
kornvarrel 0:b5bafad435c8 22
kornvarrel 0:b5bafad435c8 23 //return distance in cm
kornvarrel 0:b5bafad435c8 24 long HCSR04::distance(){
kornvarrel 0:b5bafad435c8 25 duration = echo_duration();
kornvarrel 0:b5bafad435c8 26 distance_cm = (duration/2)/29.1 ;
kornvarrel 0:b5bafad435c8 27 return distance_cm;
kornvarrel 0:b5bafad435c8 28
kornvarrel 0:b5bafad435c8 29 }