INCTRC Information Sharing Test
/
UltraSonic
大昔の超音波センサプログラム
Revision 0:f43a798a392d, committed 2018-07-14
- Comitter:
- yuron
- Date:
- Sat Jul 14 02:10:51 2018 +0000
- Commit message:
- ??????????????
Changed in this revision
diff -r 000000000000 -r f43a798a392d TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Sat Jul 14 02:10:51 2018 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/simon/code/TextLCD/#308d188a2d3a
diff -r 000000000000 -r f43a798a392d main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Jul 14 02:10:51 2018 +0000 @@ -0,0 +1,45 @@ +/* + ちょうおんぱせんさHC-SR04をしよう + LCDにきょりをひょうじ + 30cmいかのきょりならばLEDがひかる +*/ +#include "mbed.h" +#include "TextLCD.h" + +TextLCD lcd(D12, D11, D10, D9, D8, D7, TextLCD::LCD20x2); // rs, e, d4-d7 +DigitalOut trigger(D15); +DigitalOut myled(LED1); +DigitalIn echo(D14); +int distance = 0; +int correction = 0; +Timer sonar; + +int main() +{ + sonar.reset(); + while(1) { +// trigger sonar to send a ping + trigger = 1; + sonar.reset(); + wait_us(10.0); + trigger = 0; +//wait for echo high + while (echo==0) {}; +//echo high, so start timer + sonar.start(); +//wait for echo low + while (echo==1) {}; +//stop timer and read value + sonar.stop(); +//subtract software overhead timer delay and scale to cm + distance = (sonar.read_us()-correction)/58.0; + if(distance <= 30){ + myled = 1; + } else { + myled = 0; + } + lcd.printf("%d cm \n\r",distance); +//wait so that any echo(s) return before sending another ping + wait(0.2); + } +} \ No newline at end of file
diff -r 000000000000 -r f43a798a392d mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Jul 14 02:10:51 2018 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed/builds/093f2bd7b9eb \ No newline at end of file