]> git.piffa.net Git - sketchbook_andrea/blobdiff - advanced_projects/state_machine/semaforo_rgb_stimolo_millis/semaforo_rgb_stimolo_millis.ino
FSM reorder
[sketchbook_andrea] / advanced_projects / state_machine / semaforo_rgb_stimolo_millis / semaforo_rgb_stimolo_millis.ino
index a8a14c05383b2cf4b015a4f7fe4f4a333d844deb..093879babf1a6dd0188b92fbf311852e8aace2f1 100644 (file)
@@ -19,82 +19,74 @@ long timer ;
 enum states_available { // Stati della FMS
     turn_green,    // Dinamico, transizione
     green,         // Statico
-    wait_button,   // Evento - Stimolo
-    turn_yellow,      // Dinamico, transizione
     yellow,            // Statico
-    turn_red,      // Dinamico, transizione
     red            // Statico
 };
 
 states_available state  ;
+boolean wait = 0;
 
 
 void setup() {
-  pinMode(input, INPUT_PULLUP);
-  Serial.begin(9600);
-  timer = millis();
+    pinMode(input, INPUT_PULLUP);
+    Serial.begin(9600);
+    timer = millis();
 }
 
 RGBLed led(11, 10, 9); //Istanziamo un oggetto led facente parte
-                       // della classe RGBLed
+// della classe RGBLed
 
 void loop() {
-switch (state) {
+    switch (state) {
     case turn_green :
-    state = green ; // Setta il prossimo state
-    break;
+        state = green ; // Setta il prossimo state
+        break;
 
     case green:
-    led.Green();
-    if (millis() - timer >= pausa * 2/3) {
-    state = wait_button ;
-    timer += pausa * 2/3;
-    }
-    break;
+        led.Green();
+        if (wait && millis() - timer >= pausa * 2/3) {
+            state = yellow;
+            timer = millis();
+        }
 
-    case wait_button:
-    if (digitalRead(input) == LOW) { 
-    state = turn_yellow ; // Il passaggio di stato avviene alla pressione di un bottone
-    delay(20); // Debouncing, si potrebbe fare con millis()
-    timer = millis();
-    };
-    break;
+        if (digitalRead(input) == LOW) {
+            wait = 1;
+        }
+        break;
 
-    case turn_yellow :
-    state = yellow ;
-    break;
 
     case yellow :
-    led.Yellow();
-    if (millis() - timer >= pausa * 2/3) {
-    state = turn_red ;
-    timer += pausa * 2/3;
-    }
-    break;
-
-    case turn_red :
-    state = red ;
-    break;
+        led.Yellow();
+        if (millis() - timer >= pausa /3) {
+            state = red ;
+            wait = 0 ;
+            timer += pausa /3;
+        }
+        break;
 
     case red :
-    led.Red();
-    if (millis() - timer >= pausa) {
-    state = turn_green ;
-    timer += pausa ;
-    }
-    break;
+        led.Red();
+        if (millis() - timer >= pausa) {
+            state = turn_green ;
+            timer += pausa ;
+        }
+        break;
 
     default:    // In caso di default si fa giallo lampeggiante
-    led.Yellow();
-    delay(pausa/3);
-    led.Off();
-    delay(pausa/3);
-    break;
+        led.Yellow();
+        delay(pausa/3);
+        led.Off();
+        delay(pausa/3);
+        break;
 
-}
-Serial.print(millis()); 
-Serial.print(" \t Stato attuale ");
-Serial.println(state);
+    }
+
+    //Debug:
+    Serial.print(millis());
+    Serial.print(" \t Stato attuale ");
+    Serial.print(state);
+    Serial.print(" \t Wait: ");
+    Serial.println(wait);
 
 }