3 Utilizziamo la funzione millis() al posto di delay()
4 per poter gestire il lampeggio di un LED senza bloccare
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.
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.
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.
25 2015 modified by Andrea Manni
27 This example code is in the public domain.
30 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
33 // constants won't change. Used here to
35 const int ledPin = 13;
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
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)
46 // set the digital pin as output:
47 pinMode(ledPin, OUTPUT);
52 // here is where you'd put code that needs to be running all the time.
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
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?
65 // if the LED is off turn it on and vice-versa:
70 // e' possibile semplificare questa operazione?
71 // Hint: lo stato del LED e' binario: ha solo due stati possibili.
73 // set the LED with the ledState of the variable:
74 digitalWrite(ledPin, ledState);
79 1. Aggioungere un LED che brilli ogni 500ms: iniziare pensando
80 a quali variabili gestiscono l'attuale LED e a quali si
82 2. E' ora agevole cambiare gli intervalli dei due LED?
83 Modificare gli intervalli dei due led (es 500ms - 320ms)
88 3. Si sarebbe introdotto uno slip (ritardo) nei tempi dello sketch