]> git.piffa.net Git - sketchbook_andrea/blobdiff - advanced_projects/state_machine/blink_2_trans/blink_2_trans.ino
Structured data types
[sketchbook_andrea] / advanced_projects / state_machine / blink_2_trans / blink_2_trans.ino
index 9a4fa757efc5ee04edbe7ec87716a505734c900a..6545cde55bd19c47a07a8cbeacfb8e2ad3e04480 100644 (file)
@@ -4,28 +4,27 @@
   Accensione e spegnimanto di un LED utilizzando
   una FSM  con 4 stati, statici e di transizione.
 
-Costrutto switch basato su uno struct.
+Costrutto switch basato su un enum:
 
  */
 
 // Dichiarazione variabili
 int led = 11; // PWM
 int pausa = 1000;  // Variabile richiambile nel corso dell'esecuzione
-byte lum = 0 ;
+byte lum = 255 ;
 
 void setup() {
     // Inizializziamo il PIN 13 come OUTPUT
     pinMode(led, OUTPUT);
 }
 
-enum fsm_stati { // Stati della FMS
+enum fsm_stati: byte { // Stati della FMS, explicit type cast
     on,     // Statico
     to_off, // Transizione
     off,
     to_on
-};
+} stato ; // denominazione di una variabile
 
-fsm_stati stato ;
 
 void loop() {
     switch (stato) {
@@ -36,7 +35,7 @@ void loop() {
 
        // Stati
         stato = to_off ; // Setta il prossimo state
-        lum = 255;
+        // lum = 255;
         break;
 
     case to_off :
@@ -54,7 +53,7 @@ void loop() {
         delay(pausa);             
 
         stato = to_on ;
-        lum = 0;
+        // lum = 0;
         break;
 
     case to_on :
@@ -66,6 +65,9 @@ void loop() {
 
         stato = on ; // Setta il prossimo state
         break;
+
+    default: // Opzionale, quando non si verificano altre condizioni
+        break;
     }
 }
 
@@ -103,4 +105,5 @@ Esercizi successivi:
 
  1.Delay rende il codice blocking, null'altro puo' essere eseguito durante i delay
  2.Si potrebbe utilizzare millis(), vedi esercizi multitasking
+   oppure: https://www.sparkfun.com/news/1801
 */