4 Un singolo semaforo costruito col paradigma delle macchine a stato.
5 Viene utilizzato un oggetto della libreria common per gestire il LED.
7 Uno stimolo esterno rappresentato dalla pressione di un bottone
8 causa il passaggio di stato.
10 Implementata con millis() invece che con delay(),
11 sono stati aggiuntu due stati per meglio gestire lo stato yellow.
13 - Schema per un led RGB: https://lab.piffa.net/schemi/rgb.jpg
14 - Schema per un bottone: https://www.arduino.cc/en/uploads/Tutorial/inputPullupButton.png
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
31 states_available state ;
35 pinMode(input, INPUT_PULLUP);
40 RGBLed led(11, 10, 9); //Istanziamo un oggetto led facente parte
41 // della classe RGBLed
46 state = green ; // Setta il prossimo state
51 if (millis() - timer >= pausa * 2/3) {
53 timer += pausa * 2/3 ;
58 if (digitalRead(input) == LOW) {
59 state = turn_yellow ; // Il passaggio di stato avviene alla pressione di un bottone
60 delay(20); // Debouncing, si potrebbe fare con millis()
71 if (millis() - timer >= pausa / 3) {
83 if (millis() - timer >= pausa) {
89 default: // In caso di default si fa giallo lampeggiante
98 Serial.print(millis());
99 Serial.print(" \t Stato attuale ");
100 Serial.println(state);
105 1. Introdurre un secondo semaforo che cambia stato quando viene attivato
107 2. L'uso di delay() puo' essere limitativo: come rimediare?
119 2. Si potrebbe utilizzare un interrupt per gli stimoli oppure millis()
120 per gestire le pause.