]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino
70e9162162bf92ea8f40b4a83454cc7e5f2842f5
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_5_cleanup / BlinkWithoutDelay_5_cleanup.ino
1 /* Blink without Delay - Pulizia
2
3 Semplificato il ciclo condizionale
4  */
5
6 // constants won't change. Used here to 
7 // set pin numbers:
8
9 // First LED
10 int ledA =  13;      // the number of the LED pin
11 // Variables will change:
12 int ledStateA = LOW;             // ledState used to set the LED
13 long previousMillisA = 0;        // will store last time LED was updated
14
15 // Second LED data
16 int ledB = 12; //Secondo LED
17 int ledStateB = LOW;             // ledState used to set the LED
18 long previousMillisB = 0;        // will store last time LED was updated
19
20 void setup() {
21   // set the digital pin as output:
22   pinMode(ledA, OUTPUT);      
23   pinMode(ledB, OUTPUT);  
24 }
25
26 void loop()
27 {
28   lightLedA(1000);
29   lightLedB(500);
30 }
31
32 ////////////////
33 // Funzioni
34
35 void lightLedA (int interval) {
36   // Illumina il ledA secondo un intervallo passato come argomento
37
38   if(millis() - previousMillisA > interval) {
39     // save the last time you blinked the LED 
40     previousMillisA = millis();   
41
42     // if the LED is off turn it on and vice-versa:
43     ledStateA = !ledStateA ; // Inverti il LED
44   }
45   digitalWrite(ledA, ledStateA);
46 }
47
48 void lightLedB (int interval) {
49   // Illumina il ledB secondo un intervallo passato come argomento
50
51   if(millis() - previousMillisB > interval) {
52     // save the last time you blinked the LED 
53     previousMillisB = millis();   
54
55     // if the LED is off turn it on and vice-versa:
56     ledStateB = !ledStateB ; // Inverti il LED
57   }
58   digitalWrite(ledB, ledStateB);
59 }
60 /* Domande:
61  1. E' possibile avere una sola funzione che permetta di gestire 
62     qualunque LED io voglia aggiungere?
63     
64 /* Approfondimenti
65  - integrazione tra funzioni e dati: programmazione a oggetti
66  - Uso di pointers per modificare dati esterni allo scope della funzione
67  - Uso di forme di dati strutturate (array, struct) per scambiare dati tra funzioni e programma
68  */
69
70
71
72
73
74
75
76
77
78
79