]> git.piffa.net Git - sketchbook_andrea/blob - oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino
aero
[sketchbook_andrea] / oggi / BlinkWithoutDelay_1 / BlinkWithoutDelay_1.ino
1 /* Blink without Delay
2  
3  Turns on and off a light emitting diode(LED) connected to a digital  
4  pin, without using the delay() function.  This means that other code
5  can run at the same time without being interrupted by the LED code.
6  
7  The circuit:
8  * LED attached from pin 13 to ground.
9  * Note: on most Arduinos, there is already an LED on the board
10  that's attached to pin 13, so no hardware is needed for this example.
11  
12  
13  created 2005
14  by David A. Mellis
15  modified 8 Feb 2010
16  by Paul Stoffregen
17  modified by eaman
18  
19  This example code is in the public domain.
20
21  
22  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
23  */
24
25 // constants won't change. Used here to 
26 // set pin numbers:
27 const int ledPin =  13;      
28
29 // Variables will change:
30 int ledState = LOW;             // ledState used to set the LED
31 long previousMillis = 0;        // will store last time LED was updated
32
33 // the follow variables is a long because the time, measured in miliseconds,
34 // will quickly become a bigger number than can be stored in an int.
35 const long interval = 1000;           // interval at which to blink (milliseconds)
36
37 void setup() {
38   // set the digital pin as output:
39   pinMode(ledPin, OUTPUT);      
40 }
41
42 void loop()
43 {
44   // here is where you'd put code that needs to be running all the time.
45
46   // check to see if it's time to blink the LED; that is, if the 
47   // difference between the current time and last time you blinked 
48   // the LED is bigger than the interval at which you want to 
49   // blink the LED.
50  
51   if (millis() > previousMillis + interval) {
52     // Aggiorniamo il contatore previousMillis
53     previousMillis = millis();   
54
55     // if the LED is off turn it on and vice-versa:
56     if (ledState == LOW)
57       ledState = HIGH;
58     else
59       ledState = LOW;
60     // e' possibile semplificare questa operazione?
61     // Hint: lo stato del LED e' binario: ha solo due stati possibili.
62
63     // set the LED with the ledState of the variable:
64     digitalWrite(ledPin, ledState);
65   }
66 }
67
68 /* Domande
69    1. Aggioungere un LED che brilli ogni 500ms
70    2. E' ora agevole cambiare gli intervalli dei due LED? 
71       Modificare gli intervalli dei due led (es 500ms - 320ms)
72  */