5 Doppio semaforo, una via prinicipale (led) e una secondaria (secondary):
6 la via secondaria ottiene la precedenza alla pressione di un bottone.
8 Implementata con millis() invece che con delay(),
9 sono stati aggiuntu due stati per gestire lo stato yellow
10 del semafor secondario.
12 Lo sketch e' stato implementato con una sola FSM in cui si incrociano
13 gli stati dei due semafori.
18 const byte input = 2; // PIN del bottone
21 enum states_available { // Stati della FMS
22 turn_green, // Dinamico, transizione
24 wait_button, // Evento - Stimolo
25 turn_yellow, // Dinamico, transizione
27 turn_red, // Dinamico, transizione
28 turn_sec_yellow, // Yellow per semaforo secondario
33 states_available state ;
37 pinMode(input, INPUT_PULLUP);
42 RGBLed led(11, 10, 9); // Semaforo principale
43 RGBLed secondary(8,7,6); // Semaforo secondario
51 state = green ; // Setta il prossimo state
55 if (millis() - timer => pausa * 2/3) {
57 timer += pausa * 2/3 ;
62 if (digitalRead(input) == LOW) {
63 delay(20); // Debouncing, si potrebbe fare con millis()
64 state = turn_yellow ; // Il passaggio di stato avviene alla pressione di un bottone
76 if (millis() - timer >= pausa / 3) {
89 if (millis() - timer >= pausa /3) {
90 state = turn_sec_yellow ;
95 case turn_sec_yellow :
101 if (millis() - timer >= pausa / 3) {
107 default: // In caso di default si fa giallo lampeggiante
117 Serial.print(millis());
118 Serial.print(" \t Stato attuale ");
119 Serial.println(state);
124 1. E' agevole inserire degli altri semafori?
125 2. Provare a inserire un altro semafori implementando una FSM
138 1. Be' bisogna ragionare sul loro comportamente in rapporto alla FSM principale, diciamo che non e' un approccio plug and play.
139 2. Vedi esercizio successivo.