Programmi Esempio
/
4_Robot_Ultrasuoni
Ultrasuoni
main.cpp@0:ef0410ccfc84, 2017-02-09 (annotated)
- Committer:
- DonatoSt
- Date:
- Thu Feb 09 14:56:56 2017 +0000
- Revision:
- 0:ef0410ccfc84
Ultrasuoni
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DonatoSt | 0:ef0410ccfc84 | 1 | #include "mbed.h" |
DonatoSt | 0:ef0410ccfc84 | 2 | #include "hcsr04.h" |
DonatoSt | 0:ef0410ccfc84 | 3 | HCSR04 sensor (D15, D14); |
DonatoSt | 0:ef0410ccfc84 | 4 | Serial pc(USBTX, USBRX); |
DonatoSt | 0:ef0410ccfc84 | 5 | |
DonatoSt | 0:ef0410ccfc84 | 6 | |
DonatoSt | 0:ef0410ccfc84 | 7 | DigitalOut dcm01(D6); |
DonatoSt | 0:ef0410ccfc84 | 8 | DigitalOut dcm02(D7); |
DonatoSt | 0:ef0410ccfc84 | 9 | DigitalOut dcm03(D4); |
DonatoSt | 0:ef0410ccfc84 | 10 | DigitalOut dcm04(D5); |
DonatoSt | 0:ef0410ccfc84 | 11 | float dist; |
DonatoSt | 0:ef0410ccfc84 | 12 | |
DonatoSt | 0:ef0410ccfc84 | 13 | int main() |
DonatoSt | 0:ef0410ccfc84 | 14 | { |
DonatoSt | 0:ef0410ccfc84 | 15 | |
DonatoSt | 0:ef0410ccfc84 | 16 | pc.baud(115200); |
DonatoSt | 0:ef0410ccfc84 | 17 | |
DonatoSt | 0:ef0410ccfc84 | 18 | |
DonatoSt | 0:ef0410ccfc84 | 19 | while(1) { |
DonatoSt | 0:ef0410ccfc84 | 20 | sensor.start(); |
DonatoSt | 0:ef0410ccfc84 | 21 | dist=sensor.get_dist_cm(); |
DonatoSt | 0:ef0410ccfc84 | 22 | pc.printf("%dcm\t", sensor.get_dist_cm()); |
DonatoSt | 0:ef0410ccfc84 | 23 | |
DonatoSt | 0:ef0410ccfc84 | 24 | |
DonatoSt | 0:ef0410ccfc84 | 25 | if (dist > 20) { |
DonatoSt | 0:ef0410ccfc84 | 26 | |
DonatoSt | 0:ef0410ccfc84 | 27 | dcm01 = 0; |
DonatoSt | 0:ef0410ccfc84 | 28 | dcm02 = 1; |
DonatoSt | 0:ef0410ccfc84 | 29 | dcm03 = 0; |
DonatoSt | 0:ef0410ccfc84 | 30 | dcm04 = 1; |
DonatoSt | 0:ef0410ccfc84 | 31 | |
DonatoSt | 0:ef0410ccfc84 | 32 | |
DonatoSt | 0:ef0410ccfc84 | 33 | |
DonatoSt | 0:ef0410ccfc84 | 34 | } |
DonatoSt | 0:ef0410ccfc84 | 35 | |
DonatoSt | 0:ef0410ccfc84 | 36 | if (dist < 20) { |
DonatoSt | 0:ef0410ccfc84 | 37 | |
DonatoSt | 0:ef0410ccfc84 | 38 | dcm01 = 1; |
DonatoSt | 0:ef0410ccfc84 | 39 | dcm02 = 0; |
DonatoSt | 0:ef0410ccfc84 | 40 | dcm03 = 0; |
DonatoSt | 0:ef0410ccfc84 | 41 | dcm04 = 1; |
DonatoSt | 0:ef0410ccfc84 | 42 | |
DonatoSt | 0:ef0410ccfc84 | 43 | |
DonatoSt | 0:ef0410ccfc84 | 44 | |
DonatoSt | 0:ef0410ccfc84 | 45 | } |
DonatoSt | 0:ef0410ccfc84 | 46 | |
DonatoSt | 0:ef0410ccfc84 | 47 | |
DonatoSt | 0:ef0410ccfc84 | 48 | |
DonatoSt | 0:ef0410ccfc84 | 49 | |
DonatoSt | 0:ef0410ccfc84 | 50 | |
DonatoSt | 0:ef0410ccfc84 | 51 | }//Chiusa while |
DonatoSt | 0:ef0410ccfc84 | 52 | }//Chiusa main |
DonatoSt | 0:ef0410ccfc84 | 53 |