Smartage application
Dependencies: BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2
Fork of STM32L0_LoRa by
hcsr04.cpp
00001 #include "hcsr04.h" 00002 #include "mbed.h" 00003 /* 00004 *HCSR04.cpp 00005 */ 00006 HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {} 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 timer.stop(); 00019 return timer.read_us(); 00020 00021 } 00022 00023 //return distance in cm 00024 long HCSR04::distance(){ 00025 duration = echo_duration(); 00026 distance_cm = (duration/2)/29.1 ; 00027 return distance_cm; 00028 00029 } 00030
Generated on Thu Jul 14 2022 21:27:01 by 1.7.2