]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino
fb411e9aea6e0ee0687812c307d794124da7f625
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_2_led / BlinkWithoutDelay_2_led.ino
1 /* Blink without Delay - due led
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 Andrea Manni
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 ledA = 13;      // Primo LED
28 const int ledB = 12;      // Secondo LED
29
30 // Variabbili di stato
31 int ledStateA = LOW;             // ledState used to set the LED
32 int ledStateB = LOW;             // ledState used to set the LED
33               
34 long previousMillisA = 0;        // will store last time LED was updated
35 long previousMillisB = 0;        // will store last time LED was updated
36
37 // the follow variables is a long because the time, measured in miliseconds,
38 // will quickly become a bigger number than can be stored in an int.
39 long intervalA = 1000;           // interval at which to blink (milliseconds)
40 long intervalB = 500;            // interval at which to blink (milliseconds)
41
42 void setup() {
43   // set the digital pin as output:
44   pinMode(ledA, OUTPUT);      
45   pinMode(ledB, OUTPUT);  
46 }
47
48 void loop()
49 {
50 // Primo LED
51   if (millis() >= previousMillisA + intervalA) {
52     // Aggiornimo il riferimento temporale
53     previousMillisA += intervalA;
54
55     // if the LED is off turn it on and vice-versa:
56     if (ledStateA == LOW)
57       ledStateA = HIGH;
58     else
59       ledStateA = LOW;
60     // set the LED with the ledState of the variable:
61     digitalWrite(ledA, ledStateA);
62   }
63   
64 // Secondo LED
65     if (millis() >= previousMillisB + intervalB) {
66     // save the last time you blinked the LED 
67     previousMillisB += intervalB;
68
69     // if the LED is off turn it on and vice-versa:
70     if (ledStateB == LOW)
71       ledStateB = HIGH;
72     else
73       ledStateB = LOW;
74     // set the LED with the ledState of the variable:
75     digitalWrite(ledB, ledStateB);
76   }
77 }
78
79 /* Domande
80  1. Provare a isolare il codice per accendere ogni singolo led in una funzione:
81     - Quali variabili determinano il comportamento del LED?
82     - Come cambiano durante il corso dello script?
83     - Sono globali o locali? 
84     - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione?
85  */
86
87
88