]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino
408fd16b7ec1d672b193c551478e7deb714afec4
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_1 / BlinkWithoutDelay_1.ino
1 /* Blink without Delay
2  
3  Utilizziamo la funzione millis() al posto di delay()
4  per poter gestire il lampeggio di un LED senza bloccare
5  il processore.
6
7  Questo esercizio e' strutturato in una serie di passaggi incrementali
8  nei quali una versione minimale si evolve per introdurre
9  programmazione ad oggetti, interrupts, pointers.
10
11  Turns on and off a light emitting diode(LED) connected to a digital  
12  pin, without using the delay() function.  This means that other code
13  can run at the same time without being interrupted by the LED code.
14  
15  The circuit:
16  * LED attached from pin 13 to ground.
17  * Note: on most Arduinos, there is already an LED on the board
18  that's attached to pin 13, so no hardware is needed for this example.
19  
20  
21  created 2005
22  by David A. Mellis
23  modified 8 Feb 2010
24  by Paul Stoffregen
25  2015 modified by Andrea Manni
26  
27  This example code is in the public domain.
28
29  
30  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
31  */
32
33 // constants won't change. Used here to 
34 // set pin numbers:
35 const int ledPin =  13;      
36
37 // Variables will change:
38 int ledState = LOW;             // ledState used to set the LED
39 long previousMillis = 0;        // will store last time LED was updated
40
41 // the follow variables is a long because the time, measured in miliseconds,
42 // will quickly become a bigger number than can be stored in an int.
43 const long interval = 1000;           // interval at which to blink (milliseconds)
44
45 void setup() {
46   // set the digital pin as output:
47   pinMode(ledPin, OUTPUT);      
48 }
49
50 void loop()
51 {
52   // here is where you'd put code that needs to be running all the time.
53
54   // check to see if it's time to blink the LED; that is, if the 
55   // difference between the current time and last time you blinked 
56   // the LED is bigger than the interval at which you want to 
57   // blink the LED.
58  
59   if (millis() >= previousMillis + interval) {
60     // Aggiorniamo il contatore previousMillis
61     previousMillis += interval ; 
62     // previousMillis = millis(); // 3) Cosa succederebbe se fosse
63     // passato piu' di 1ms dall'evento all'azione?
64
65     // if the LED is off turn it on and vice-versa:
66     if (ledState == LOW)
67       ledState = HIGH;
68     else
69       ledState = LOW;
70     // e' possibile semplificare questa operazione?
71     // Hint: lo stato del LED e' binario: ha solo due stati possibili.
72
73     // set the LED with the ledState of the variable:
74     digitalWrite(ledPin, ledState);
75   }
76 }
77
78 /* Domande
79    1. Aggioungere un LED che brilli ogni 500ms: iniziare pensando
80       a quali variabili gestiscono l'attuale LED e a quali si
81       dovranno aggiungere.
82    2. E' ora agevole cambiare gli intervalli dei due LED? 
83       Modificare gli intervalli dei due led (es 500ms - 320ms)
84
85
86
87    Risposta
88    3. Si sarebbe introdotto uno slip (ritardo) nei tempi dello sketch
89
90  */