]> git.piffa.net Git - sketchbook_andrea/blobdiff - multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino
multi cleanup
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_3_funzione / BlinkWithoutDelay_3_funzione.ino
index 886dd5e98142f2af7055dd4f252f2def385e8b52..a77facafd1dd0069b8cd7eec38e1ea66b141d6a1 100644 (file)
@@ -1,32 +1,36 @@
-/* Blink without Delay
+/* Blink without Delay: Refactoring
  
Soluzione
Blink con funzione
  
 3. Provare a isolare il codice per accendere ogni singolo led in una funzione:
-    Quali variabili determinano il comportamento del LED?
-    Sono globali o locali?
-    
- */
Soluzione: Provare a isolare il codice per accendere ogni singolo led in una funzione:
+    - Quali variabili determinano il comportamento del LED?
+    - Come cambiano durante il corso dello script?
+    - Sono globali o locali? 
 
-// constants won't change. Used here to 
-// set pin numbers:
+Variabili: http://www.maffucci.it/2011/12/15/appunti-di-programmazione-su-arduino-variabili/ 
+
+ */
 
 /////////////
 // First LED
-int ledA =  13;      // the number of the LED pin
+const int ledA =  13;       // the number of the LED pin
 // Variables will change:
-int ledStateA = LOW;             // ledState used to set the LED
-long previousMillisA = 0;        // will store last time LED was updated
+int ledStateA = LOW;        // ledState used to set the LED
+long previousMillisA = 0;   // will store last time LED was updated
 // the follow variables is a long because the time, measured in miliseconds,
 // will quickly become a bigger number than can be stored in an int.
-long intervalA = 1000;           // interval at which to blink (milliseconds)
+long intervalA = 1000;      // interval at which to blink (milliseconds)
+void lightLedA () ;
 
 //////////////
-// Second LED
-int ledB = 12; //Secondo LED
-int ledStateB = LOW;             // ledState used to set the LED
-long previousMillisB = 0;        // will store last time LED was updated
-long intervalB = 500;           // interval at which to blink (milliseconds)
+//  Second LED
+// Now with less global variables thanks to static (see function body)
+const int ledB = 12; //Secondo LED
+                     // ledState used to set the LED
+long previousMillisB = 0;    // will store last time LED was updated
+                             // interval at which to blink (milliseconds)
+void lightLedB () ;
 
 
 void setup() {
@@ -42,10 +46,10 @@ void loop()
 }
 
 
-// Funzioni
+// Funzioni:
 
 void lightLedA () {
-  if(millis() - previousMillisA > intervalA) {
+  if (millis() > previousMillisA + intervalA) {
     // save the last time you blinked the LED 
     previousMillisA = millis();   
 
@@ -61,7 +65,9 @@ void lightLedA () {
 }
 
 void lightLedB () {
-  if(millis() - previousMillisB > intervalB) {
+  long intervalB = 500;
+   static int ledStateB ;  // https://www.arduino.cc/en/Reference/Static
+  if (millis() > previousMillisB + intervalB) {
     // save the last time you blinked the LED 
     previousMillisB = millis();   
 
@@ -78,11 +84,12 @@ void lightLedB () {
 
 /* Domande
  1. Modificare le funzioni in modo che accettino come parametro
   l'intervallo di lampeggio.
-
+ l'intervallo di lampeggio.
  */
 
 
 
 
 
+