mbed.hだけで超音波センサの制御できるかも
Dependencies: mbed
Revision 0:05cd466c355a, committed 2018-03-28
- Comitter:
- ponpoko1939
- Date:
- Wed Mar 28 10:08:10 2018 +0000
- Commit message:
- var1
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 05cd466c355a main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 28 10:08:10 2018 +0000 @@ -0,0 +1,34 @@ +#include "mbed.h" + +DigitalOut TRIG(p7); +DigitalIn ECHO(p8); +Timer t; //Using timer0 +Serial pc(USBTX,USBRX); + +double duration,distance; + +int main() { + while(1){ +start: + while(ECHO == 0) { + TRIG = 1; + wait_us(10); + TRIG = 0; + wait_us(300); + t.start(); + } + t.stop(); + + duration = t.read_us(); + + if(duration > 0){ + duration = duration / 2; + distance = duration*340*100; + } + else { + pc.printf("error"); + goto start; + } + pc.printf("Distance:%f\n",distance); + } +}
diff -r 000000000000 -r 05cd466c355a mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Mar 28 10:08:10 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/994bdf8177cb \ No newline at end of file