aggiornato con servomotore e display
Dependencies: mbed Servo mbed-rtos beep hcsr04 TextLCD
main.cpp@5:8eb27286a3e3, 2019-03-28 (annotated)
- Committer:
- CiroSamu
- Date:
- Thu Mar 28 14:17:45 2019 +0000
- Revision:
- 5:8eb27286a3e3
- Parent:
- 4:48754f3702d7
- Child:
- 6:0088253be231
prog
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
huntR | 0:650a24681467 | 1 | #include "mbed.h" |
huntR | 0:650a24681467 | 2 | #include "rtos.h" |
huntR | 0:650a24681467 | 3 | #include "hcsr04.h" |
huntR | 0:650a24681467 | 4 | #include "beep.h" |
CiroSamu | 4:48754f3702d7 | 5 | #define trigger1 D15 |
CiroSamu | 4:48754f3702d7 | 6 | #define echo1 D14 |
CiroSamu | 4:48754f3702d7 | 7 | #define trigger2 D12 |
CiroSamu | 4:48754f3702d7 | 8 | #define echo2 D13 |
huntR | 0:650a24681467 | 9 | /******************************/ |
huntR | 0:650a24681467 | 10 | //bluetooth |
CiroSamu | 2:33e2a092cab6 | 11 | Serial pc(USBTX, USBRX);//tx e rx |
huntR | 0:650a24681467 | 12 | /******************************/ |
huntR | 0:650a24681467 | 13 | //sensori ad ultrasuoni |
CiroSamu | 4:48754f3702d7 | 14 | HCSR04 sensor1(trigger1, echo1); |
CiroSamu | 4:48754f3702d7 | 15 | HCSR04 sensor2(trigger2, echo2); |
CiroSamu | 2:33e2a092cab6 | 16 | //HCSR04 sensor3(D11, D9); |
huntR | 0:650a24681467 | 17 | /******************************/ |
CiroSamu | 5:8eb27286a3e3 | 18 | DigitalIn mybutton(USER_BUTTON); |
CiroSamu | 5:8eb27286a3e3 | 19 | //Thread thread_sensori; // istanzia oggetto thread |
altoimperatore | 1:13556d8b4a34 | 20 | |
CiroSamu | 5:8eb27286a3e3 | 21 | bool occupata[3] = {false,false,false}; |
huntR | 0:650a24681467 | 22 | /******************************/ |
CiroSamu | 5:8eb27286a3e3 | 23 | void button_thread(void const *args){ |
CiroSamu | 4:48754f3702d7 | 24 | |
CiroSamu | 4:48754f3702d7 | 25 | while(1){ |
CiroSamu | 4:48754f3702d7 | 26 | if (mybutton==0){ |
CiroSamu | 5:8eb27286a3e3 | 27 | int i=0; |
CiroSamu | 5:8eb27286a3e3 | 28 | do{ |
CiroSamu | 5:8eb27286a3e3 | 29 | i++; |
CiroSamu | 5:8eb27286a3e3 | 30 | if(occupata[i]== false){ |
CiroSamu | 5:8eb27286a3e3 | 31 | pc.printf("il parcheggio numero %d e libero\n",i); |
CiroSamu | 5:8eb27286a3e3 | 32 | |
CiroSamu | 5:8eb27286a3e3 | 33 | }//else if(occupata[1]==true && occupata[2]==true) pc.printf("tutti i parcheggi sono occupati"); |
CiroSamu | 5:8eb27286a3e3 | 34 | |
CiroSamu | 5:8eb27286a3e3 | 35 | }while(occupata[i]== true && i<2); |
CiroSamu | 5:8eb27286a3e3 | 36 | } |
CiroSamu | 4:48754f3702d7 | 37 | |
CiroSamu | 4:48754f3702d7 | 38 | |
CiroSamu | 4:48754f3702d7 | 39 | } |
CiroSamu | 5:8eb27286a3e3 | 40 | } |
huntR | 0:650a24681467 | 41 | //sensore semaforo 1 |
huntR | 0:650a24681467 | 42 | /******************************/ |
huntR | 0:650a24681467 | 43 | void sensori(){ |
CiroSamu | 5:8eb27286a3e3 | 44 | float distanza1; |
CiroSamu | 5:8eb27286a3e3 | 45 | float distanza2; |
huntR | 0:650a24681467 | 46 | sensor1.start(); |
huntR | 0:650a24681467 | 47 | wait_ms(100); |
CiroSamu | 5:8eb27286a3e3 | 48 | sensor2.start(); //partono i sensori |
CiroSamu | 5:8eb27286a3e3 | 49 | wait_ms(100); |
CiroSamu | 5:8eb27286a3e3 | 50 | distanza1= sensor1.get_dist_cm() ; |
CiroSamu | 5:8eb27286a3e3 | 51 | distanza2=sensor2.get_dist_cm(); |
CiroSamu | 5:8eb27286a3e3 | 52 | pc.printf("%.0fcm\r\n%.0fcm\r\n", distanza1,distanza2); |
CiroSamu | 5:8eb27286a3e3 | 53 | if(sensor1.get_dist_cm() < 5 && occupata[1] == false) //se uno dei sensori capta una distanza minore 10 |
altoimperatore | 1:13556d8b4a34 | 54 | occupata[1] = true; |
altoimperatore | 1:13556d8b4a34 | 55 | else |
altoimperatore | 1:13556d8b4a34 | 56 | occupata[1] = false; |
CiroSamu | 5:8eb27286a3e3 | 57 | |
CiroSamu | 5:8eb27286a3e3 | 58 | if(sensor2.get_dist_cm() < 5 && occupata[2] == false) //se uno dei sensori capta una distanza minore a 10 e gianello è false |
CiroSamu | 5:8eb27286a3e3 | 59 | occupata[2] = true; |
CiroSamu | 5:8eb27286a3e3 | 60 | else |
CiroSamu | 5:8eb27286a3e3 | 61 | occupata[2] = false; |
CiroSamu | 2:33e2a092cab6 | 62 | } |
altoimperatore | 1:13556d8b4a34 | 63 | |
huntR | 0:650a24681467 | 64 | int main(){ |
CiroSamu | 4:48754f3702d7 | 65 | // pc.printf("prova"); |
altoimperatore | 3:272fb57c7fcb | 66 | //while(true) { |
CiroSamu | 4:48754f3702d7 | 67 | //Thread button_th(button_thread); |
CiroSamu | 5:8eb27286a3e3 | 68 | //int i=0; |
CiroSamu | 5:8eb27286a3e3 | 69 | |
CiroSamu | 5:8eb27286a3e3 | 70 | Thread button_th(button_thread); |
CiroSamu | 5:8eb27286a3e3 | 71 | while(1){ |
CiroSamu | 5:8eb27286a3e3 | 72 | sensori(); |
CiroSamu | 5:8eb27286a3e3 | 73 | } |
CiroSamu | 4:48754f3702d7 | 74 | |
CiroSamu | 2:33e2a092cab6 | 75 | // blue.printf("prova"); |
CiroSamu | 4:48754f3702d7 | 76 | // if (mybutton==0) { |
CiroSamu | 5:8eb27286a3e3 | 77 | /* |
CiroSamu | 5:8eb27286a3e3 | 78 | do{ |
CiroSamu | 5:8eb27286a3e3 | 79 | i++; |
CiroSamu | 5:8eb27286a3e3 | 80 | if(occupata[i]== false){ |
CiroSamu | 5:8eb27286a3e3 | 81 | pc.printf("il parcheggio numero %d è libero\n",i); |
CiroSamu | 5:8eb27286a3e3 | 82 | |
CiroSamu | 5:8eb27286a3e3 | 83 | } |
CiroSamu | 5:8eb27286a3e3 | 84 | |
CiroSamu | 5:8eb27286a3e3 | 85 | |
CiroSamu | 5:8eb27286a3e3 | 86 | }while(occupata[i]== true && i<2); |
CiroSamu | 5:8eb27286a3e3 | 87 | |
CiroSamu | 5:8eb27286a3e3 | 88 | */ |
CiroSamu | 5:8eb27286a3e3 | 89 | /*if(occupata[i]== false && occupata[2]== false) pc.printf("il parcheggio numero %d è libero\n", i + 1 ); |
CiroSamu | 4:48754f3702d7 | 90 | else if(occupata[0]== false && occupata[1]== true){ |
CiroSamu | 4:48754f3702d7 | 91 | pc.printf("il parcheggio numero %d è libero\n", i + 1 ); |
CiroSamu | 4:48754f3702d7 | 92 | break; |
CiroSamu | 4:48754f3702d7 | 93 | } else if(occupata[0]== true && occupata[1]== false){ |
CiroSamu | 4:48754f3702d7 | 94 | pc.printf("il parcheggio numero %d è libero\n", i + 2 ); |
CiroSamu | 4:48754f3702d7 | 95 | break; |
CiroSamu | 4:48754f3702d7 | 96 | }else{ |
CiroSamu | 4:48754f3702d7 | 97 | pc.printf("appiennt\n"); |
CiroSamu | 4:48754f3702d7 | 98 | break; |
CiroSamu | 5:8eb27286a3e3 | 99 | }*/ |
CiroSamu | 5:8eb27286a3e3 | 100 | // break; |
CiroSamu | 5:8eb27286a3e3 | 101 | // } |
altoimperatore | 1:13556d8b4a34 | 102 | |
altoimperatore | 1:13556d8b4a34 | 103 | |
CiroSamu | 4:48754f3702d7 | 104 | //} |
altoimperatore | 1:13556d8b4a34 | 105 | |
altoimperatore | 1:13556d8b4a34 | 106 | |
CiroSamu | 4:48754f3702d7 | 107 | |
CiroSamu | 5:8eb27286a3e3 | 108 | |
altoimperatore | 1:13556d8b4a34 | 109 | |
CiroSamu | 2:33e2a092cab6 | 110 | //void thread sensori() { |
CiroSamu | 5:8eb27286a3e3 | 111 | |
CiroSamu | 5:8eb27286a3e3 | 112 | } |
CiroSamu | 2:33e2a092cab6 | 113 |