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