slave funzionante
Dependencies: HCSR04 beep mbed-rtos mbed
Diff: main.cpp
- Revision:
- 0:ffca8ab7f43f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Apr 28 19:45:21 2017 +0000 @@ -0,0 +1,160 @@ +#include "mbed.h" +#include "rtos.h" +#include "hcsr04.h" +#include "beep.h" +//dichiarazioni let e sensori +//sensori ad ultrasuoni +/******************************/ +//bluetooth +Serial blue(D10,D2); +/******************************/ +HCSR04 sensor1(D15, D14); +HCSR04 sensor2(D12, D13); +//HCSR04 sensor3(D11, D9); +/******************************/ +//Led per le macchine +DigitalOut rossoC(PA_0);//led +DigitalOut gialloC(D5); +DigitalOut verdeC(D3); +/******************************/ +//buzzer +Beep buzzer(D4); +/******************************/ +//led per i pedoni +DigitalOut rossoP(D6); +DigitalOut gialloP(D7); +DigitalOut verdeP(D8); +/******************************/ +/******************************/ +//dichiarazione funzioni +void giallo(); +void semafori(); +/******************************/ +int i=0; +char riceviBlue = '0'; +bool gianello=false; +bool visto = false; +int cont =0; +/******************************/ + +//sensore semaforo 1 +/******************************/ +/******************************/ +void sensori(){ + while(!visto && !gianello){ + sensor1.start(); + wait_ms(100); + sensor2.start(); + wait_ms(100); + if(sensor1.get_dist_cm()<10||sensor2.get_dist_cm()<10){ + wait(4); + sensor1.start(); + wait_ms(100); + sensor2.start(); + wait_ms(100); + if(sensor1.get_dist_cm()<10||sensor2.get_dist_cm()<10&&!gianello){ + visto = true; + blue.putc('B'); + wait_ms(100); + semafori(); + visto=false; + } + } + } +} +/******************************/ + +//controllo borini +/******************************/ +void iezzo (void const *args){ + while(true){ + if(blue.readable()){ + riceviBlue = blue.getc(); + } + if(riceviBlue == 'A') + { + gianello=true; + riceviBlue='0'; + semafori(); + riceviBlue='0'; + gianello=false; + visto=false; + } + } +} +/******************************/ + +int main(){ + //inizializzazione + /******************************/ + rossoP=1; + verdeP=0; + gialloP=0; + gialloC=0; + rossoC=0; + verdeC=1; + blue.baud(38400); + Thread th1(iezzo); + /******************************/ + while(1){ + if(!gianello){ + wait_ms(500); + sensori(); + } + } +} +void giallo(){ + //giallo per i pedoni e rosso per le macchine + + int n; + do{ + n=0; + verdeP=0; + gialloP=1; + //bippata del buzzer + /******************************/ + for(i=0;i<10;i++){ + buzzer.beep(500,0.1); + gialloP=1; + wait_ms(200); + buzzer.nobeep(); + buzzer.beep(1000,0.1); + gialloP=1; + gialloP=1; + wait_ms(200); + buzzer.nobeep(); + gialloP=1; + } + /******************************/ + wait_ms(100); + }while(n==1); + wait_ms(100); +} +void semafori(){ + //giallo per le macchine, rosso per i pedoni + verdeC=0; + gialloC=1; + rossoC=0; + wait(4); + //rosso per le macchine, verde per i pedoni + buzzer.beep(1200,50); + gialloC=0; + rossoC=1; + rossoP=0; + verdeP=1; + wait(10); + buzzer.nobeep(); + //giallo per i pedoni e rosso per le macchine + do{ + giallo(); + riceviBlue=blue.getc(); + + }while(riceviBlue=='L'); + cont = 0; + //rosso ai pedoni e verde alle automobili + gialloP=0; + rossoP=1; + rossoC=0; + verdeC=1; + wait_ms(5000); +} \ No newline at end of file