primjer mjerenja udaljenosti
Dependencies: mbed
Fork of HC-SR04 by
main.cpp@1:144bd7bdd1d1, 2015-06-12 (annotated)
- Committer:
- cvitas
- Date:
- Fri Jun 12 16:37:50 2015 +0000
- Revision:
- 1:144bd7bdd1d1
- Parent:
- 0:e7a1440e980a
Pomo? uz zadatak 6
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); |
cvitas | 1:144bd7bdd1d1 | 8 | DigitalOut led1 (LED1); |
Goji | 0:e7a1440e980a | 9 | //Pswitch sw(p8); |
Goji | 0:e7a1440e980a | 10 | double pulsedur, distance; |
Goji | 0:e7a1440e980a | 11 | Timer tmr; |
Goji | 0:e7a1440e980a | 12 | |
Goji | 0:e7a1440e980a | 13 | int main() |
Goji | 0:e7a1440e980a | 14 | { |
Goji | 0:e7a1440e980a | 15 | pc.printf("start\n"); |
Goji | 0:e7a1440e980a | 16 | while(1) { |
Goji | 0:e7a1440e980a | 17 | tmr.reset(); |
Goji | 0:e7a1440e980a | 18 | trig = 1; |
Goji | 0:e7a1440e980a | 19 | wait_us(10); |
Goji | 0:e7a1440e980a | 20 | trig = 0; |
Goji | 0:e7a1440e980a | 21 | |
Goji | 0:e7a1440e980a | 22 | while (!echo); // wait high |
Goji | 0:e7a1440e980a | 23 | tmr.start(); |
Goji | 0:e7a1440e980a | 24 | |
Goji | 0:e7a1440e980a | 25 | while (echo); // wait low |
Goji | 0:e7a1440e980a | 26 | tmr.stop(); |
Goji | 0:e7a1440e980a | 27 | |
Goji | 0:e7a1440e980a | 28 | pulsedur = tmr.read_us(); |
Goji | 0:e7a1440e980a | 29 | distance = pulsedur*0.017; //cm |
cvitas | 1:144bd7bdd1d1 | 30 | pc.printf("pulsedur=%8.2f\n\r", distance); |
cvitas | 1:144bd7bdd1d1 | 31 | pc.printf("\n"); |
cvitas | 1:144bd7bdd1d1 | 32 | if (distance<4) |
cvitas | 1:144bd7bdd1d1 | 33 | led1 =1; |
cvitas | 1:144bd7bdd1d1 | 34 | else |
cvitas | 1:144bd7bdd1d1 | 35 | led1 =0; |
cvitas | 1:144bd7bdd1d1 | 36 | wait(0.5); |
Goji | 0:e7a1440e980a | 37 | } |
Goji | 0:e7a1440e980a | 38 | } |