Lavoro terminato
Dependencies: mbed Servo mbed-rtos beep hcsr04 TextLCD
main.cpp@9:5be4a23d1e86, 2019-04-04 (annotated)
- Committer:
- CiroSamu
- Date:
- Thu Apr 04 12:55:52 2019 +0000
- Revision:
- 9:5be4a23d1e86
- Parent:
- 8:5fc14e646e91
- Child:
- 10:2fd7e922103f
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 | 7:8ad50e27d002 | 5 | #include "TextLCD.h" |
CiroSamu | 7:8ad50e27d002 | 6 | #include "Servo.h" |
CiroSamu | 9:5be4a23d1e86 | 7 | #define trigger1 PC_8 //sensore 1 |
CiroSamu | 9:5be4a23d1e86 | 8 | #define echo1 PC_6 |
CiroSamu | 9:5be4a23d1e86 | 9 | #define trigger2 D12 //sensore 2 |
CiroSamu | 4:48754f3702d7 | 10 | #define echo2 D13 |
CiroSamu | 9:5be4a23d1e86 | 11 | #define trigger3 PC_10 //sensore 3 |
CiroSamu | 9:5be4a23d1e86 | 12 | #define echo3 PC_12 |
CiroSamu | 9:5be4a23d1e86 | 13 | #define trigger4 D6 //sensore 4 |
CiroSamu | 9:5be4a23d1e86 | 14 | #define echo4 D8 |
huntR | 0:650a24681467 | 15 | /******************************/ |
huntR | 0:650a24681467 | 16 | //bluetooth |
altoimperatore | 8:5fc14e646e91 | 17 | Serial pc(USBTX, USBRX); //istanza oggetto porta seriale |
altoimperatore | 8:5fc14e646e91 | 18 | Servo servo(D9); //istanza oggetto servomotore |
altoimperatore | 8:5fc14e646e91 | 19 | TextLCD lcd(D10,D11,D5,D4,D3,D2); //istanza oggetto schermo |
huntR | 0:650a24681467 | 20 | /******************************/ |
huntR | 0:650a24681467 | 21 | //sensori ad ultrasuoni |
altoimperatore | 8:5fc14e646e91 | 22 | |
altoimperatore | 8:5fc14e646e91 | 23 | HCSR04 sensor1(trigger1, echo1); //istanza oggetto sensore a distanza |
CiroSamu | 4:48754f3702d7 | 24 | HCSR04 sensor2(trigger2, echo2); |
altoimperatore | 8:5fc14e646e91 | 25 | HCSR04 sensor3(trigger3, echo3); |
CiroSamu | 9:5be4a23d1e86 | 26 | HCSR04 sensor4(trigger4, echo4); |
huntR | 0:650a24681467 | 27 | /******************************/ |
CiroSamu | 9:5be4a23d1e86 | 28 | DigitalIn mybutton(D7); //istanza oggetto bottone |
CiroSamu | 5:8eb27286a3e3 | 29 | //Thread thread_sensori; // istanzia oggetto thread |
altoimperatore | 1:13556d8b4a34 | 30 | |
CiroSamu | 9:5be4a23d1e86 | 31 | bool occupata[5] = { false, false, false,false}; |
CiroSamu | 9:5be4a23d1e86 | 32 | int numero_parcheggi = 4; |
altoimperatore | 8:5fc14e646e91 | 33 | |
altoimperatore | 8:5fc14e646e91 | 34 | //dichiarazione vettore booleano che indica gli stati del parcheggio |
altoimperatore | 8:5fc14e646e91 | 35 | /*******************************************************************************************************/ |
altoimperatore | 8:5fc14e646e91 | 36 | /* stato del parcheggio : |
altoimperatore | 8:5fc14e646e91 | 37 | occupata = false; il parcheggio è libero |
altoimperatore | 8:5fc14e646e91 | 38 | occupata = true; il parcheggio è occupato */ |
altoimperatore | 8:5fc14e646e91 | 39 | |
huntR | 0:650a24681467 | 40 | /******************************/ |
altoimperatore | 8:5fc14e646e91 | 41 | void button_thread(void const *args) |
altoimperatore | 8:5fc14e646e91 | 42 | { |
altoimperatore | 8:5fc14e646e91 | 43 | bool sos = false; |
altoimperatore | 8:5fc14e646e91 | 44 | int angle = 0; |
altoimperatore | 8:5fc14e646e91 | 45 | |
altoimperatore | 8:5fc14e646e91 | 46 | while(1) |
altoimperatore | 8:5fc14e646e91 | 47 | { |
altoimperatore | 8:5fc14e646e91 | 48 | if (mybutton == 0) |
altoimperatore | 8:5fc14e646e91 | 49 | { |
CiroSamu | 6:0088253be231 | 50 | |
altoimperatore | 8:5fc14e646e91 | 51 | int i = 0; |
CiroSamu | 6:0088253be231 | 52 | sos = false; |
altoimperatore | 8:5fc14e646e91 | 53 | |
CiroSamu | 5:8eb27286a3e3 | 54 | do{ |
altoimperatore | 8:5fc14e646e91 | 55 | if (i < numero_parcheggi) i++; |
altoimperatore | 8:5fc14e646e91 | 56 | else break; |
altoimperatore | 8:5fc14e646e91 | 57 | |
altoimperatore | 8:5fc14e646e91 | 58 | if(occupata[i] == false) |
altoimperatore | 8:5fc14e646e91 | 59 | { |
altoimperatore | 8:5fc14e646e91 | 60 | pc.printf("il parcheggio numero %d e libero\r\n", i); //stampa quali parcheggi sono liberi |
altoimperatore | 8:5fc14e646e91 | 61 | lcd.printf("il parcheggio numero %d e libero\r\n", i); |
altoimperatore | 8:5fc14e646e91 | 62 | sos = true; //se un parcheggio è libero conseguentemente non possono essere tutti occupati |
CiroSamu | 9:5be4a23d1e86 | 63 | while(1) |
altoimperatore | 8:5fc14e646e91 | 64 | { |
CiroSamu | 9:5be4a23d1e86 | 65 | servo.write(angle); //non toccare |
CiroSamu | 9:5be4a23d1e86 | 66 | angle += 10; |
CiroSamu | 9:5be4a23d1e86 | 67 | if(angle > 90) |
CiroSamu | 9:5be4a23d1e86 | 68 | { |
CiroSamu | 9:5be4a23d1e86 | 69 | wait(3); |
CiroSamu | 9:5be4a23d1e86 | 70 | while(angle != 0 ) |
CiroSamu | 9:5be4a23d1e86 | 71 | { |
CiroSamu | 9:5be4a23d1e86 | 72 | servo.write(angle); |
CiroSamu | 9:5be4a23d1e86 | 73 | angle -= 10; |
CiroSamu | 9:5be4a23d1e86 | 74 | wait(0.25); |
CiroSamu | 9:5be4a23d1e86 | 75 | } |
CiroSamu | 9:5be4a23d1e86 | 76 | if(angle == 0) break; |
CiroSamu | 9:5be4a23d1e86 | 77 | } |
CiroSamu | 9:5be4a23d1e86 | 78 | wait(0.25); |
CiroSamu | 9:5be4a23d1e86 | 79 | } |
altoimperatore | 8:5fc14e646e91 | 80 | //if(occupata[1] == false && occupata[2] == false) break; // if(occupata[1]==true && occupata[2]==true) //sos = !sos; |
CiroSamu | 6:0088253be231 | 81 | }/*else if(sos==false && i==2){ |
CiroSamu | 6:0088253be231 | 82 | pc.printf("tutti i parcheggi sono occupati"); |
CiroSamu | 6:0088253be231 | 83 | break; |
CiroSamu | 6:0088253be231 | 84 | }//else if(occupata[1]==true && occupata[2]==true) pc.printf("tutti i parcheggi sono occupati"); |
CiroSamu | 6:0088253be231 | 85 | */ |
altoimperatore | 8:5fc14e646e91 | 86 | |
CiroSamu | 9:5be4a23d1e86 | 87 | } while(1); // ciclo infinito, segnalera sempre i parcheggi liberi |
CiroSamu | 6:0088253be231 | 88 | // if(occupata[1]==true && occupata[2]==true) pc.printf("tutti i parcheggi sono occupati"); |
altoimperatore | 8:5fc14e646e91 | 89 | if (sos == false) |
altoimperatore | 8:5fc14e646e91 | 90 | { |
altoimperatore | 8:5fc14e646e91 | 91 | pc.printf("tutti i parcheggi sono occupati\r\n"); |
altoimperatore | 8:5fc14e646e91 | 92 | lcd.printf("tutti i parcheggi sono occupati\r\n"); |
altoimperatore | 8:5fc14e646e91 | 93 | } |
altoimperatore | 8:5fc14e646e91 | 94 | else |
altoimperatore | 8:5fc14e646e91 | 95 | { |
altoimperatore | 8:5fc14e646e91 | 96 | lcd.printf("per riavviare premere l'apposito pulsante"); |
altoimperatore | 8:5fc14e646e91 | 97 | pc.printf("per riavviare premere l'apposito pulsante"); |
altoimperatore | 8:5fc14e646e91 | 98 | } |
altoimperatore | 8:5fc14e646e91 | 99 | }//if pulsante |
CiroSamu | 4:48754f3702d7 | 100 | |
CiroSamu | 4:48754f3702d7 | 101 | |
altoimperatore | 8:5fc14e646e91 | 102 | }//chiusura iterazione del pulsante |
altoimperatore | 8:5fc14e646e91 | 103 | }// chiusura funzione |
huntR | 0:650a24681467 | 104 | //sensore semaforo 1 |
huntR | 0:650a24681467 | 105 | /******************************/ |
altoimperatore | 8:5fc14e646e91 | 106 | void sensori() |
altoimperatore | 8:5fc14e646e91 | 107 | { |
CiroSamu | 9:5be4a23d1e86 | 108 | /*float distanza1; |
CiroSamu | 9:5be4a23d1e86 | 109 | float distanza2; |
CiroSamu | 9:5be4a23d1e86 | 110 | float distanza3;*/ |
huntR | 0:650a24681467 | 111 | sensor1.start(); |
huntR | 0:650a24681467 | 112 | wait_ms(100); |
CiroSamu | 5:8eb27286a3e3 | 113 | sensor2.start(); //partono i sensori |
CiroSamu | 5:8eb27286a3e3 | 114 | wait_ms(100); |
altoimperatore | 8:5fc14e646e91 | 115 | sensor3.start(); |
altoimperatore | 8:5fc14e646e91 | 116 | wait_ms(100); |
CiroSamu | 9:5be4a23d1e86 | 117 | sensor4.start(); |
CiroSamu | 9:5be4a23d1e86 | 118 | wait_ms(100); |
CiroSamu | 9:5be4a23d1e86 | 119 | /* distanza1=sensor1.get_dist_cm(); |
CiroSamu | 9:5be4a23d1e86 | 120 | distanza2=sensor2.get_dist_cm(); |
CiroSamu | 9:5be4a23d1e86 | 121 | distanza3=sensor3.get_dist_cm(); |
CiroSamu | 9:5be4a23d1e86 | 122 | pc.printf("sensore1 %.0fcm\r\n\nsensore2 %.0fcm\r\n\nsensore3 %.0fcm\r\n\n", distanza1,distanza2,distanza3);*/ |
CiroSamu | 9:5be4a23d1e86 | 123 | if(sensor1.get_dist_cm() <= 10.0) occupata[1] = true; |
CiroSamu | 9:5be4a23d1e86 | 124 | else occupata[1] = false; |
CiroSamu | 5:8eb27286a3e3 | 125 | |
CiroSamu | 9:5be4a23d1e86 | 126 | if(sensor2.get_dist_cm() <= 10.0) occupata[2] = true; |
altoimperatore | 8:5fc14e646e91 | 127 | else occupata[2] = false; |
altoimperatore | 8:5fc14e646e91 | 128 | |
CiroSamu | 9:5be4a23d1e86 | 129 | if(sensor3.get_dist_cm() <= 10.0) occupata[3] = true; |
altoimperatore | 8:5fc14e646e91 | 130 | else occupata[3] = false; |
CiroSamu | 9:5be4a23d1e86 | 131 | |
CiroSamu | 9:5be4a23d1e86 | 132 | if(sensor4.get_dist_cm() <= 10.0) occupata[4] = true; |
CiroSamu | 9:5be4a23d1e86 | 133 | else occupata[4] = false; |
CiroSamu | 9:5be4a23d1e86 | 134 | |
CiroSamu | 9:5be4a23d1e86 | 135 | |
altoimperatore | 8:5fc14e646e91 | 136 | } |
altoimperatore | 1:13556d8b4a34 | 137 | |
huntR | 0:650a24681467 | 138 | int main(){ |
CiroSamu | 4:48754f3702d7 | 139 | // pc.printf("prova"); |
altoimperatore | 3:272fb57c7fcb | 140 | //while(true) { |
CiroSamu | 4:48754f3702d7 | 141 | //Thread button_th(button_thread); |
CiroSamu | 5:8eb27286a3e3 | 142 | //int i=0; |
CiroSamu | 5:8eb27286a3e3 | 143 | |
altoimperatore | 8:5fc14e646e91 | 144 | Thread button_th(button_thread); |
altoimperatore | 8:5fc14e646e91 | 145 | while(1) |
altoimperatore | 8:5fc14e646e91 | 146 | { |
CiroSamu | 5:8eb27286a3e3 | 147 | sensori(); |
CiroSamu | 5:8eb27286a3e3 | 148 | } |
CiroSamu | 5:8eb27286a3e3 | 149 | } |
CiroSamu | 2:33e2a092cab6 | 150 |