/*
Semaforo RGB
+
+Version: singolo semaforo + millis + memoria giallo
Un singolo semaforo costruito col paradigma delle macchine a stato.
Viene utilizzato un oggetto della libreria common per gestire il LED.
Uno stimolo esterno rappresentato dalla pressione di un bottone
causa il passaggio di stato.
+ Il semaforo resta verde fino a quando non riceve lo stimolo
+ (es passaggio pedonale).
Implementata con millis() invece che con delay(),
- sono stati aggiuntu due stati per meglio gestire lo stato yellow.
*/
const byte input = 2; // PIN del bottone
int pausa = 3000;
long timer ;
+boolean wait = 0; // Memoria bottone
+
enum states_available { // Stati della FMS
- turn_green, // Dinamico, transizione
green, // Statico
yellow, // Statico
red // Statico
};
states_available state ;
-boolean wait = 0;
void setup() {
timer = millis();
}
-RGBLed led(11, 10, 9); //Istanziamo un oggetto led facente parte
-// della classe RGBLed
+RGBLed led(11, 10, 9);
void loop() {
switch (state) {
- case turn_green :
- state = green ; // Setta il prossimo state
- break;
case green:
led.Green();
- if (wait && millis() - timer >= pausa * 2/3) {
+ if (wait && (millis() - timer >= pausa * 2/3)) {
state = yellow;
- timer += pausa * 2/3;
+ timer = millis();
}
if (digitalRead(input) == LOW) {
case red :
led.Red();
if (millis() - timer >= pausa) {
- state = turn_green ;
+ state = green ;
timer += pausa ;
}
break;