]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino
Clean up multitasking, bottoni con pooling e interrupts
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_3_funzione / BlinkWithoutDelay_3_funzione.ino
1 /* Blink without Delay: Refactoring
2  
3  Blink con funzione
4  
5  Soluzione: Provare a isolare il codice per accendere ogni singolo led in una funzione:
6  
7     - Quali variabili determinano il comportamento del LED?
8     - Come cambiano durante il corso dello script?
9     - Sono globali o locali? 
10
11 Variabili: http://www.maffucci.it/2011/12/15/appunti-di-programmazione-su-arduino-variabili/ 
12
13  */
14
15 /////////////
16 // First LED
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)
21 void lightLedA () ;
22
23 //////////////
24 //  Second LED
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)
29 void lightLedB () ;
30
31
32 void setup() {
33   // set the digital pin as output:
34   pinMode(ledA, OUTPUT);      
35   pinMode(ledB, OUTPUT);  
36 }
37
38 void loop()
39 {
40   lightLedA();
41   lightLedB();
42 }
43
44
45 // Funzioni:
46
47 void lightLedA () {
48   if (millis() - previousMillisA >=  intervalA) {
49     // save the last time you blinked the LED 
50     previousMillisA += intervalA;
51
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);
56   }
57
58 }
59
60 void lightLedB () {
61   long intervalB = 500;
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 ;
66
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);
71   }
72 }
73
74
75 /* Domande
76  1. Modificare le funzioni in modo che accettino come parametro
77  l'intervallo di lampeggio.
78  
79  */
80
81
82
83
84
85