]> git.piffa.net Git - sketchbook_andrea/blobdiff - advanced_projects/state_machine/semaforo_rgb/semaforo_rgb.ino
PWM Pulse
[sketchbook_andrea] / advanced_projects / state_machine / semaforo_rgb / semaforo_rgb.ino
index 1fce582ecdbb59ca58dd4aaaab429902e5aa4f0b..5d86aa95778c48f47bf4a858b90fa03bb3f1db35 100644 (file)
@@ -1,14 +1,17 @@
 /*
    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() {
@@ -33,20 +36,16 @@ 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) {
@@ -67,7 +66,7 @@ void loop() {
     case red :
         led.Red();
         if (millis() - timer >= pausa) {
-            state = turn_green ;
+            state = green ;
             timer += pausa ;
         }
         break;