Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos tsi_sensor SLCD
HCSR04.h
00001 #ifndef HCSR04_H 00002 #define HCSR04_H 00003 00004 // The following lines MUST be added into the main() routine 00005 /* 00006 HCSR04_timer.start(); 00007 HCSR04_echo.rise(&echo_start); 00008 HCSR04_echo.fall(&echo_end); 00009 HCSR04_start.attach(&pulse_trigger, 100E-3); 00010 */ 00011 00012 DigitalOut HCSR04_trigger(PTC13); 00013 InterruptIn HCSR04_echo(PTC16); 00014 Ticker HCSR04_start; 00015 Timer HCSR04_timer; 00016 float HCSR04_time, HCSR04_inches, HCSR04_cm; 00017 00018 void HCSR04_pulse_trigger() 00019 { 00020 HCSR04_trigger.write(1); 00021 wait(10E-6); 00022 HCSR04_trigger.write(0); 00023 } 00024 00025 void HCSR04_echo_start() 00026 { 00027 HCSR04_timer.reset(); 00028 } 00029 00030 void HCSR04_echo_end() 00031 { 00032 HCSR04_time=HCSR04_timer.read(); 00033 if(HCSR04_time < 30E-3) // delay longer than 30 ms indicates no echo 00034 { 00035 HCSR04_inches=HCSR04_time*13503.9*0.5; // speed of sound = 13503.9 inches/second and distance to object is 1/2 path length; 00036 HCSR04_cm=HCSR04_time*34300.0*0.5; // speed of sound = 34300 cm/second and distance to object is 1/2 path length; 00037 } 00038 else 00039 { 00040 HCSR04_inches=-1.0; 00041 HCSR04_cm=-1.0; 00042 } 00043 } 00044 00045 00046 #endif //HCSR04_H
Generated on Thu Jul 21 2022 08:33:46 by
1.7.2