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.
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 00004 float measure(); 00005 00006 int main() 00007 { 00008 00009 TextLCD lcd(PTE30, PTE29, PTE23, PTE22, PTE21, PTE20); // rs, e, d4-d7 00010 float distance; 00011 00012 while(1) { 00013 distance = measure(); 00014 lcd.cls(); 00015 lcd.printf("Distance = %.2f in.", distance); 00016 wait(0.5); 00017 } 00018 } 00019 00020 // HCSR04 "driver" 00021 float measure() 00022 { 00023 Timer timer; 00024 long time; 00025 DigitalOut trigger(PTB9); 00026 DigitalIn echo(PTB8); 00027 00028 timer.reset(); 00029 00030 // Ensure HCSR04 is idle. 00031 trigger = 0; 00032 wait (0.060); 00033 00034 // Trigger the HCSR04. 00035 trigger = 1; 00036 wait_us(10); 00037 trigger = 0; 00038 00039 // Wait for the HCSR04's measurement cycle to begin. 00040 while (echo == 0) 00041 ; 00042 00043 // Start measuring. 00044 timer.start(); 00045 while (echo == 1) 00046 ; 00047 timer.stop(); 00048 00049 time = timer.read_us(); 00050 00051 if (time > 30000) // Delay at maximum range plus some slop. 00052 return -1.0; // Didn't receive an echo. 00053 else 00054 return 0.006756 * time; 00055 }
Generated on Thu Aug 4 2022 11:36:19 by
1.7.2