]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino
cleanup multitasking
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_3_funzione / BlinkWithoutDelay_3_funzione.ino
1 /* Blink without Delay
2  
3  Soluzione
4  
5   3. Provare a isolare il codice per accendere ogni singolo led in una funzione:
6     Quali variabili determinano il comportamento del LED?
7     Sono globali o locali?
8     
9  */
10
11 // constants won't change. Used here to 
12 // set pin numbers:
13
14 /////////////
15 // First LED
16 int ledA =  13;      // the number of the LED pin
17 // Variables will change:
18 int ledStateA = LOW;             // ledState used to set the LED
19 long previousMillisA = 0;        // will store last time LED was updated
20 // the follow variables is a long because the time, measured in miliseconds,
21 // will quickly become a bigger number than can be stored in an int.
22 long intervalA = 1000;           // interval at which to blink (milliseconds)
23
24 //////////////
25 // Second LED
26 int ledB = 12; //Secondo LED
27 int ledStateB = LOW;             // ledState used to set the LED
28 long previousMillisB = 0;        // will store last time LED was updated
29 long intervalB = 500;           // interval at which to blink (milliseconds)
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 = millis();   
51
52     // if the LED is off turn it on and vice-versa:
53     if (ledStateA == LOW)
54       ledStateA = HIGH;
55     else
56       ledStateA = LOW;
57     // set the LED with the ledState of the variable:
58     digitalWrite(ledA, ledStateA);
59   }
60
61 }
62
63 void lightLedB () {
64   if(millis() - previousMillisB > intervalB) {
65     // save the last time you blinked the LED 
66     previousMillisB = millis();   
67
68     // if the LED is off turn it on and vice-versa:
69     if (ledStateB == LOW)
70       ledStateB = HIGH;
71     else
72       ledStateB = LOW;
73     // set the LED with the ledState of the variable:
74     digitalWrite(ledB, ledStateB);
75   }
76 }
77
78
79 /* Domande
80  1. Modificare le funzioni in modo che accettino come parametro
81     l'intervallo di lampeggio.
82
83  */
84
85
86
87
88