]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino
a5db67b3cb71611d4bb69ac263eb2358e86070fd
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_3_funzione / BlinkWithoutDelay_3_funzione.ino
1 /* Blink without Delay
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  */
12
13 /////////////
14 // First LED
15 int ledA =  13;      // the number of the LED pin
16 // Variables will change:
17 int ledStateA = LOW;             // ledState used to set the LED
18 long previousMillisA = 0;        // will store last time LED was updated
19 // the follow variables is a long because the time, measured in miliseconds,
20 // will quickly become a bigger number than can be stored in an int.
21 long intervalA = 1000;           // interval at which to blink (milliseconds)
22 void lightLedA () ;
23
24 //////////////
25 // Second LED
26 int ledB = 12; //Secondo LED
27            // ledState used to set the LED
28 long previousMillisB = 0;        // will store last time LED was updated
29            // interval at which to blink (milliseconds)
30 void lightLedB () ;
31
32
33 void setup() {
34   // set the digital pin as output:
35   pinMode(ledA, OUTPUT);      
36   pinMode(ledB, OUTPUT);  
37 }
38
39 void loop()
40 {
41   lightLedA();
42   lightLedB();
43 }
44
45
46 // Funzioni:
47
48 void lightLedA () {
49   if(millis() - previousMillisA > intervalA) {
50     // save the last time you blinked the LED 
51     previousMillisA = millis();   
52
53     // if the LED is off turn it on and vice-versa:
54     if (ledStateA == LOW)
55       ledStateA = HIGH;
56     else
57       ledStateA = LOW;
58     // set the LED with the ledState of the variable:
59     digitalWrite(ledA, ledStateA);
60   }
61
62 }
63
64 void lightLedB () {
65   long intervalB = 500;
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 = millis();   
70
71     // if the LED is off turn it on and vice-versa:
72     if (ledStateB == LOW)
73       ledStateB = HIGH;
74     else
75       ledStateB = LOW;
76     // set the LED with the ledState of the variable:
77     digitalWrite(ledB, ledStateB);
78   }
79 }
80
81
82 /* Domande
83  1. Modificare le funzioni in modo che accettino come parametro
84  l'intervallo di lampeggio.
85  
86  */
87
88
89
90
91
92