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 /* 00002 ちょうおんぱせんさHC-SR04をしよう 00003 LCDにきょりをひょうじ 00004 30cmいかのきょりならばLEDがひかる 00005 */ 00006 #include "mbed.h" 00007 #include "TextLCD.h" 00008 00009 TextLCD lcd(D12, D11, D10, D9, D8, D7, TextLCD::LCD20x2); // rs, e, d4-d7 00010 DigitalOut trigger(D15); 00011 DigitalOut myled(LED1); 00012 DigitalIn echo(D14); 00013 int distance = 0; 00014 int correction = 0; 00015 Timer sonar; 00016 00017 int main() 00018 { 00019 sonar.reset(); 00020 while(1) { 00021 // trigger sonar to send a ping 00022 trigger = 1; 00023 sonar.reset(); 00024 wait_us(10.0); 00025 trigger = 0; 00026 //wait for echo high 00027 while (echo==0) {}; 00028 //echo high, so start timer 00029 sonar.start(); 00030 //wait for echo low 00031 while (echo==1) {}; 00032 //stop timer and read value 00033 sonar.stop(); 00034 //subtract software overhead timer delay and scale to cm 00035 distance = (sonar.read_us()-correction)/58.0; 00036 if(distance <= 30){ 00037 myled = 1; 00038 } else { 00039 myled = 0; 00040 } 00041 lcd.printf("%d cm \n\r",distance); 00042 //wait so that any echo(s) return before sending another ping 00043 wait(0.2); 00044 } 00045 }
Generated on Mon Jul 25 2022 23:07:25 by
1.7.2