primjer mjerenja udaljenosti
Dependencies: mbed
Fork of HC-SR04 by
main.cpp@0:e7a1440e980a, 2015-01-28 (annotated)
- Committer:
- Goji
- Date:
- Wed Jan 28 15:17:47 2015 +0000
- Revision:
- 0:e7a1440e980a
- Child:
- 1:144bd7bdd1d1
HC-SR04 test for @nanaju_
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Goji | 0:e7a1440e980a | 1 | #include "mbed.h" |
Goji | 0:e7a1440e980a | 2 | //#include "Pswitch.h" |
Goji | 0:e7a1440e980a | 3 | |
Goji | 0:e7a1440e980a | 4 | Serial pc(USBTX, USBRX); // tx, rx |
Goji | 0:e7a1440e980a | 5 | |
Goji | 0:e7a1440e980a | 6 | DigitalOut trig (p25); |
Goji | 0:e7a1440e980a | 7 | DigitalIn echo (p6); |
Goji | 0:e7a1440e980a | 8 | //Pswitch sw(p8); |
Goji | 0:e7a1440e980a | 9 | double pulsedur, distance; |
Goji | 0:e7a1440e980a | 10 | Timer tmr; |
Goji | 0:e7a1440e980a | 11 | |
Goji | 0:e7a1440e980a | 12 | int main() |
Goji | 0:e7a1440e980a | 13 | { |
Goji | 0:e7a1440e980a | 14 | pc.printf("start\n"); |
Goji | 0:e7a1440e980a | 15 | while(1) { |
Goji | 0:e7a1440e980a | 16 | tmr.reset(); |
Goji | 0:e7a1440e980a | 17 | trig = 1; |
Goji | 0:e7a1440e980a | 18 | wait_us(10); |
Goji | 0:e7a1440e980a | 19 | trig = 0; |
Goji | 0:e7a1440e980a | 20 | |
Goji | 0:e7a1440e980a | 21 | while (!echo); // wait high |
Goji | 0:e7a1440e980a | 22 | tmr.start(); |
Goji | 0:e7a1440e980a | 23 | |
Goji | 0:e7a1440e980a | 24 | while (echo); // wait low |
Goji | 0:e7a1440e980a | 25 | tmr.stop(); |
Goji | 0:e7a1440e980a | 26 | |
Goji | 0:e7a1440e980a | 27 | pulsedur = tmr.read_us(); |
Goji | 0:e7a1440e980a | 28 | distance = pulsedur*0.017; //cm |
Goji | 0:e7a1440e980a | 29 | pc.printf("pulsedur=%8.2f\n", distance); |
Goji | 0:e7a1440e980a | 30 | wait(0.3); |
Goji | 0:e7a1440e980a | 31 | } |
Goji | 0:e7a1440e980a | 32 | } |