1 /* Blink without Delay: Refactoring
5 Soluzione: Provare a isolare il codice per accendere ogni singolo led in una funzione:
7 - Quali variabili determinano il comportamento del LED?
8 - Come cambiano durante il corso dello script?
9 - Sono globali o locali?
11 Variabili: http://www.maffucci.it/2011/12/15/appunti-di-programmazione-su-arduino-variabili/
17 const int ledA = 13; // the number of the LED pin
18 int ledStateA = LOW; // ledState used to set the LED
19 long previousMillisA = 0; // will store last time LED was updated
20 long intervalA = 1000; // interval at which to blink (milliseconds)
25 // Ora con meno variabili globali utilizzando static (vedi corpo della funzione)
26 const int ledB = 12; //Secondo LED
27 long previousMillisB = 0; // will store last time LED was updated
28 // interval at which to blink (milliseconds)
33 // set the digital pin as output:
34 pinMode(ledA, OUTPUT);
35 pinMode(ledB, OUTPUT);
48 if (millis() - previousMillisA >= intervalA) {
49 // save the last time you blinked the LED
50 previousMillisA += intervalA;
52 // if the LED is off turn it on and vice-versa:
53 ledStateA = !ledStateA ;
54 // set the LED with the ledState of the variable:
55 digitalWrite(ledA, ledStateA);
62 static int ledStateB ; // https://www.arduino.cc/en/Reference/Static
63 if (millis() - previousMillisB >= intervalB) {
64 // save the last time you blinked the LED
65 previousMillisB += intervalB ;
67 // if the LED is off turn it on and vice-versa:
68 ledStateB = !ledStateB;
69 // set the LED with the ledState of the variable:
70 digitalWrite(ledB, ledStateB);
76 1. Modificare le funzioni in modo che accettino come parametro
77 l'intervallo di lampeggio.