2 Semaforo RGB single FSM
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.
15 - Schema per un led RGB: https://lab.piffa.net/schemi/rgb.jpg
16 - Schema per un bottone: https://www.arduino.cc/en/uploads/Tutorial/inputPullupButton.png
20 const byte input = 2; // PIN del bottone
23 enum states_available { // Stati della FMS
24 turn_green, // Dinamico, transizione
26 wait_button, // Evento - Stimolo
27 turn_yellow, // Dinamico, transizione
29 turn_red, // Dinamico, transizione
30 turn_sec_yellow,// Yellow per semaforo secondario
31 sec_yellow, // Statico
35 states_available state ;
39 pinMode(input, INPUT_PULLUP);
44 RGBLed led(11, 10, 9); // Semaforo principale
45 RGBLed secondary(8,7,6); // Semaforo secondario
53 state = green ; // Setta il prossimo state
57 if (millis() - timer => pausa * 2/3) {
59 timer += pausa * 2/3 ;
64 if (digitalRead(input) == LOW) {
65 delay(20); // Debouncing, si potrebbe fare con millis()
66 state = turn_yellow ; // Il passaggio di stato avviene alla pressione di un bottone
78 if (millis() - timer >= pausa / 3) {
91 if (millis() - timer >= pausa /3) {
92 state = turn_sec_yellow ;
97 case turn_sec_yellow :
103 if (millis() - timer >= pausa / 3) {
109 default: // In caso di default si fa giallo lampeggiante
119 Serial.print(millis());
120 Serial.print(" \t Stato attuale ");
121 Serial.println(state);
126 1. E' agevole inserire degli altri semafori?
127 2. Provare a inserire un altro semafori implementando una FSM
140 1. Be' bisogna ragionare sul loro comportamente in rapporto alla FSM principale, diciamo che non e' un approccio plug and play.
141 2. Vedi esercizio successivo.