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 // Variables will change:
19 int ledStateA = LOW; // ledState used to set the LED
20 long previousMillisA = 0; // will store last time LED was updated
21 // the follow variables is a long because the time, measured in miliseconds,
22 // will quickly become a bigger number than can be stored in an int.
23 long intervalA = 1000; // interval at which to blink (milliseconds)
28 // Now with less global variables thanks to static (see function body)
29 const int ledB = 12; //Secondo LED
30 // ledState used to set the LED
31 long previousMillisB = 0; // will store last time LED was updated
32 // interval at which to blink (milliseconds)
37 // set the digital pin as output:
38 pinMode(ledA, OUTPUT);
39 pinMode(ledB, OUTPUT);
52 if (millis() - previousMillisA >= intervalA) {
53 // save the last time you blinked the LED
54 previousMillisA += intervalA;
56 // if the LED is off turn it on and vice-versa:
57 ledStateA = !ledStateA ;
58 // set the LED with the ledState of the variable:
59 digitalWrite(ledA, ledStateA);
66 static int ledStateB ; // https://www.arduino.cc/en/Reference/Static
67 if (millis() - previousMillisB >= intervalB) {
68 // save the last time you blinked the LED
69 previousMillisB += intervalB ;
71 // if the LED is off turn it on and vice-versa:
72 ledStateB = !ledStateB;
73 // set the LED with the ledState of the variable:
74 digitalWrite(ledB, ledStateB);
80 1. Modificare le funzioni in modo che accettino come parametro
81 l'intervallo di lampeggio.