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