]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino
multitasking
[sketchbook_andrea] / multitasking / 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  
18  This example code is in the public domain.
19
20  
21  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
22  */
23
24 // constants won't change. Used here to 
25 // set pin numbers:
26 const int ledPin =  13;      // the number of the LED pin
27
28 // Variables will change:
29 int ledState = LOW;             // ledState used to set the LED
30 long previousMillis = 0;        // will store last time LED was updated
31
32 // the follow variables is a long because the time, measured in miliseconds,
33 // will quickly become a bigger number than can be stored in an int.
34 long interval = 1000;           // interval at which to blink (milliseconds)
35
36 void setup() {
37   // set the digital pin as output:
38   pinMode(ledPin, OUTPUT);      
39 }
40
41 void loop()
42 {
43   // here is where you'd put code that needs to be running all the time.
44
45   // check to see if it's time to blink the LED; that is, if the 
46   // difference between the current time and last time you blinked 
47   // the LED is bigger than the interval at which you want to 
48   // blink the LED.
49   unsigned long currentMillis = millis();
50  
51   if(currentMillis - previousMillis > interval) {
52     // save the last time you blinked the LED 
53     previousMillis = currentMillis;   
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
61     // set the LED with the ledState of the variable:
62     digitalWrite(ledPin, ledState);
63   }
64 }
65
66 /* Domande
67    1. Aggioungere un LED che brilli ogni 500ms
68    2. E' possibile cambiare gli intervalli dei due LED?
69    */