]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_2_1_led_funzione/BlinkWithoutDelay_2_1_led_funzione.ino
7b812ee08455b996a6ed4f7b0cfc5276a6b16990
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_2_1_led_funzione / BlinkWithoutDelay_2_1_led_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 // First LED
15 const int ledA =  13;      // the number of the LED pin
16
17 // Variables will change:
18 int ledStateA = LOW;             // ledState used to set the LED
19
20 long previousMillisA = 0;        // will store last time LED was updated
21
22 // the follow variables is a long because the time, measured in miliseconds,
23 // will quickly become a bigger number than can be stored in an int.
24 long intervalA = 1000;           // interval at which to blink (milliseconds)
25
26 // Second LED
27 int ledB = 12; //Secondo LED
28 int ledStateB = LOW;             // ledState used to set the LED
29 long previousMillisB = 0;        // will store last time LED was updated
30 long intervalB = 500;           // interval at which to blink (milliseconds)
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   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  2. Inserire un secondo LED con intervallo 500ms
82  1. Trasformare il codice utilizzato in una State Machine
83  */
84
85
86
87
88