dodany czujnik

Fork of HCSR04 by Antoniolinux B.

Committer:
jguzik
Date:
Fri Jun 17 08:54:31 2016 +0000
Revision:
1:0e7a98b41a75
Parent:
0:86b2086be101

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antoniolinux 0:86b2086be101 1 #include "hcsr04.h"
antoniolinux 0:86b2086be101 2 #include "mbed.h"
antoniolinux 0:86b2086be101 3 /*
antoniolinux 0:86b2086be101 4 *HCSR04.cpp
antoniolinux 0:86b2086be101 5 */
jguzik 1:0e7a98b41a75 6 HCSR04::HCSR04(PinName t, PinName e, long to) : trig(t), echo(e), time_out(to){}
antoniolinux 0:86b2086be101 7 long HCSR04::echo_duration() {
antoniolinux 0:86b2086be101 8
antoniolinux 0:86b2086be101 9 timer.reset(); //reset timer
antoniolinux 0:86b2086be101 10 trig=0; // trigger low
antoniolinux 0:86b2086be101 11 wait_us(2); // wait
antoniolinux 0:86b2086be101 12 trig=1; // trigger high
antoniolinux 0:86b2086be101 13 wait_us(10);
antoniolinux 0:86b2086be101 14 trig=0; // trigger low
jguzik 1:0e7a98b41a75 15 while(!echo); // start pulseIN
jguzik 1:0e7a98b41a75 16 timer.start();
jguzik 1:0e7a98b41a75 17 while(echo)
jguzik 1:0e7a98b41a75 18 {
jguzik 1:0e7a98b41a75 19 if(timer.read_us()>time_out)
jguzik 1:0e7a98b41a75 20 break;
jguzik 1:0e7a98b41a75 21 }
jguzik 1:0e7a98b41a75 22 timer.stop();
jguzik 1:0e7a98b41a75 23
jguzik 1:0e7a98b41a75 24 long czas=timer.read_us();
jguzik 1:0e7a98b41a75 25 if(czas<300)
jguzik 1:0e7a98b41a75 26 return 0;
jguzik 1:0e7a98b41a75 27 return timer.read_us();
antoniolinux 0:86b2086be101 28
antoniolinux 0:86b2086be101 29 }
antoniolinux 0:86b2086be101 30
antoniolinux 0:86b2086be101 31 //return distance in cm
jguzik 1:0e7a98b41a75 32 long HCSR04::distance(){
antoniolinux 0:86b2086be101 33 duration = echo_duration();
jguzik 1:0e7a98b41a75 34 distance_cm = (duration*34)/1000/2 ;
jguzik 1:0e7a98b41a75 35 return distance_cm;
antoniolinux 0:86b2086be101 36
antoniolinux 0:86b2086be101 37 }