dodany czujnik
Fork of HCSR04 by
Embed:
(wiki syntax)
Show/hide line numbers
hcsr04.cpp
00001 #include "hcsr04.h" 00002 #include "mbed.h" 00003 /* 00004 *HCSR04.cpp 00005 */ 00006 HCSR04::HCSR04(PinName t, PinName e, long to) : trig(t), echo(e), time_out(to){} 00007 long HCSR04::echo_duration() { 00008 00009 timer.reset(); //reset timer 00010 trig=0; // trigger low 00011 wait_us(2); // wait 00012 trig=1; // trigger high 00013 wait_us(10); 00014 trig=0; // trigger low 00015 while(!echo); // start pulseIN 00016 timer.start(); 00017 while(echo) 00018 { 00019 if(timer.read_us()>time_out) 00020 break; 00021 } 00022 timer.stop(); 00023 00024 long czas=timer.read_us(); 00025 if(czas<300) 00026 return 0; 00027 return timer.read_us(); 00028 00029 } 00030 00031 //return distance in cm 00032 long HCSR04::distance(){ 00033 duration = echo_duration(); 00034 distance_cm = (duration*34)/1000/2 ; 00035 return distance_cm; 00036 00037 }
Generated on Mon Aug 1 2022 15:37:37 by 1.7.2