semaforo

Dependencies:   mbed mbed-rtos beep HCSR04

Committer:
huntR
Date:
Fri Apr 28 19:35:48 2017 +0000
Revision:
0:650a24681467
Master

Who changed what in which revision?

UserRevisionLine numberNew 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"
huntR 0:650a24681467 5 /******************************/
huntR 0:650a24681467 6 //bluetooth
huntR 0:650a24681467 7 Serial blue(D10,D2);//tx e rx
huntR 0:650a24681467 8 /******************************/
huntR 0:650a24681467 9 //sensori ad ultrasuoni
huntR 0:650a24681467 10 HCSR04 sensor1(D15, D14);
huntR 0:650a24681467 11 HCSR04 sensor2(D12, D13);
huntR 0:650a24681467 12 HCSR04 sensor3(D11, D9);
huntR 0:650a24681467 13 /******************************/
huntR 0:650a24681467 14 //dichiarazioni led
huntR 0:650a24681467 15 //Led per le macchine
huntR 0:650a24681467 16 DigitalOut rossoC(PA_0);
huntR 0:650a24681467 17 DigitalOut gialloC(D5);
huntR 0:650a24681467 18 DigitalOut verdeC(D3);
huntR 0:650a24681467 19 /******************************/
huntR 0:650a24681467 20 //led per i pedoni
huntR 0:650a24681467 21 DigitalOut rossoP(D6);
huntR 0:650a24681467 22 DigitalOut gialloP(D7);
huntR 0:650a24681467 23 DigitalOut verdeP(D8);
huntR 0:650a24681467 24 /******************************/
huntR 0:650a24681467 25 //buzzer
huntR 0:650a24681467 26 Beep buzzer(D4);
huntR 0:650a24681467 27 /******************************/
huntR 0:650a24681467 28 /******************************/
huntR 0:650a24681467 29 //dichiarazione funzioni
huntR 0:650a24681467 30 void giallo();
huntR 0:650a24681467 31 void semafori();
huntR 0:650a24681467 32 /******************************/
huntR 0:650a24681467 33 //variabili
huntR 0:650a24681467 34 int i=0;
huntR 0:650a24681467 35 char riceviBlue = '0';
huntR 0:650a24681467 36 bool gianello= false;
huntR 0:650a24681467 37 bool visto = false;
huntR 0:650a24681467 38 /******************************/
huntR 0:650a24681467 39
huntR 0:650a24681467 40 //sensore semaforo 1
huntR 0:650a24681467 41 /******************************/
huntR 0:650a24681467 42 void sensori(){
huntR 0:650a24681467 43 while(!visto && !gianello){
huntR 0:650a24681467 44 sensor1.start();
huntR 0:650a24681467 45 wait_ms(100);
huntR 0:650a24681467 46 sensor2.start();
huntR 0:650a24681467 47 wait_ms(100);
huntR 0:650a24681467 48 if(sensor1.get_dist_cm()<10||sensor2.get_dist_cm()<10){
huntR 0:650a24681467 49 wait(4);
huntR 0:650a24681467 50 sensor1.start();
huntR 0:650a24681467 51 wait_ms(100);
huntR 0:650a24681467 52 sensor2.start();
huntR 0:650a24681467 53 wait_ms(100);
huntR 0:650a24681467 54 if(sensor1.get_dist_cm()<10||sensor2.get_dist_cm()<10&&!gianello){
huntR 0:650a24681467 55 visto = true;
huntR 0:650a24681467 56 blue.putc('A');
huntR 0:650a24681467 57 wait_ms(100);
huntR 0:650a24681467 58 semafori();
huntR 0:650a24681467 59 visto=false;
huntR 0:650a24681467 60 }
huntR 0:650a24681467 61 }
huntR 0:650a24681467 62 }
huntR 0:650a24681467 63 }
huntR 0:650a24681467 64 /******************************/
huntR 0:650a24681467 65 //controllo borini
huntR 0:650a24681467 66 /******************************/
huntR 0:650a24681467 67 void borini (void const *args){
huntR 0:650a24681467 68 while(true){
huntR 0:650a24681467 69 if(blue.readable()){
huntR 0:650a24681467 70 riceviBlue = blue.getc();
huntR 0:650a24681467 71 }
huntR 0:650a24681467 72 if(riceviBlue == 'B')
huntR 0:650a24681467 73 {
huntR 0:650a24681467 74 gianello=true;
huntR 0:650a24681467 75 riceviBlue='0';
huntR 0:650a24681467 76 semafori();
huntR 0:650a24681467 77 riceviBlue='0';
huntR 0:650a24681467 78 gianello=false;
huntR 0:650a24681467 79 visto=false;
huntR 0:650a24681467 80 }
huntR 0:650a24681467 81 }
huntR 0:650a24681467 82 }
huntR 0:650a24681467 83 /******************************/
huntR 0:650a24681467 84 int main(){
huntR 0:650a24681467 85 //inizializzazione
huntR 0:650a24681467 86 /******************************/
huntR 0:650a24681467 87 rossoP=1;
huntR 0:650a24681467 88 verdeP=0;
huntR 0:650a24681467 89 gialloP=0;
huntR 0:650a24681467 90 gialloC=0;
huntR 0:650a24681467 91 rossoC=0;
huntR 0:650a24681467 92 verdeC=1;
huntR 0:650a24681467 93 blue.baud(38400);
huntR 0:650a24681467 94 /******************************/
huntR 0:650a24681467 95 Thread th1(borini);
huntR 0:650a24681467 96 while(1){
huntR 0:650a24681467 97 if(!gianello){
huntR 0:650a24681467 98 sensori();
huntR 0:650a24681467 99 }
huntR 0:650a24681467 100 }
huntR 0:650a24681467 101 }
huntR 0:650a24681467 102 void giallo(){
huntR 0:650a24681467 103 int n;
huntR 0:650a24681467 104 int cont =0;
huntR 0:650a24681467 105 do{
huntR 0:650a24681467 106 n=0;
huntR 0:650a24681467 107 verdeP=0;
huntR 0:650a24681467 108 gialloP=1;
huntR 0:650a24681467 109 //bippata del buzzer
huntR 0:650a24681467 110 /******************************/
huntR 0:650a24681467 111 for(i=0;i<10;i++){
huntR 0:650a24681467 112 buzzer.beep(1500,50);
huntR 0:650a24681467 113 wait_ms(200);
huntR 0:650a24681467 114 buzzer.nobeep();
huntR 0:650a24681467 115 buzzer.beep(2000,50);
huntR 0:650a24681467 116 wait_ms(200);
huntR 0:650a24681467 117 buzzer.nobeep();
huntR 0:650a24681467 118 }
huntR 0:650a24681467 119 /******************************/
huntR 0:650a24681467 120 sensor3.start();
huntR 0:650a24681467 121 wait_ms(100);
huntR 0:650a24681467 122 if(sensor3.get_dist_cm()<48){
huntR 0:650a24681467 123 cont++;
huntR 0:650a24681467 124 if(cont<=2)
huntR 0:650a24681467 125 blue.putc('L');
huntR 0:650a24681467 126 n=1;
huntR 0:650a24681467 127 }
huntR 0:650a24681467 128 wait_ms(100);
huntR 0:650a24681467 129 }while(n==1&&cont<=2);
huntR 0:650a24681467 130 blue.putc('0');
huntR 0:650a24681467 131 wait_ms(200);
huntR 0:650a24681467 132 }
huntR 0:650a24681467 133 void semafori(){
huntR 0:650a24681467 134 //giallo per le macchine, rosso per i pedoni
huntR 0:650a24681467 135 verdeC=0;
huntR 0:650a24681467 136 gialloC=1;
huntR 0:650a24681467 137 rossoC=0;
huntR 0:650a24681467 138 wait(4);
huntR 0:650a24681467 139 //rosso per le macchine, verde per i pedoni
huntR 0:650a24681467 140 buzzer.beep(1200,50);
huntR 0:650a24681467 141 gialloC=0;
huntR 0:650a24681467 142 rossoC=1;
huntR 0:650a24681467 143 rossoP=0;
huntR 0:650a24681467 144 verdeP=1;
huntR 0:650a24681467 145 wait(10);
huntR 0:650a24681467 146 buzzer.nobeep();
huntR 0:650a24681467 147 //giallo per i pedoni e rosso per le macchine
huntR 0:650a24681467 148 giallo();
huntR 0:650a24681467 149 //rosso ai pedoni e verde alle automobili
huntR 0:650a24681467 150 gialloP=0;
huntR 0:650a24681467 151 rossoP=1;
huntR 0:650a24681467 152 rossoC=0;
huntR 0:650a24681467 153 verdeC=1;
huntR 0:650a24681467 154 wait_ms(5000);
huntR 0:650a24681467 155 }