]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino
Multitasking
[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 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 ledA =  13;      // the number of the LED pin
28 int ledB = 12; //Secondo LED
29
30 // Variables will change:
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   // here is where you'd put code that needs to be running all the time.
51
52   // check to see if it's time to blink the LED; that is, if the 
53   // difference between the current time and last time you blinked 
54   // the LED is bigger than the interval at which you want to 
55   // blink the LED.
56
57 // First LED
58   if(millis() - previousMillisA > intervalA) {
59     // save the last time you blinked the LED 
60     previousMillisA = millis();   
61
62     // if the LED is off turn it on and vice-versa:
63     if (ledStateA == LOW)
64       ledStateA = HIGH;
65     else
66       ledStateA = LOW;
67     // set the LED with the ledState of the variable:
68     digitalWrite(ledA, ledStateA);
69   }
70   
71 // Second LED
72     if(millis() - previousMillisB > intervalB) {
73     // save the last time you blinked the LED 
74     previousMillisB = millis();   
75
76     // if the LED is off turn it on and vice-versa:
77     if (ledStateB == LOW)
78       ledStateB = HIGH;
79     else
80       ledStateB = LOW;
81     // set the LED with the ledState of the variable:
82     digitalWrite(ledB, ledStateB);
83   }
84 }
85
86 /* Domande
87  1. Provare a isolare il codice per accendere ogni singolo led in una funzione:
88     - Quali variabili determinano il comportamento del LED?
89     - Come cambiano durante il corso dello script?
90     - Sono globali o locali? 
91     - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione?
92  */
93
94
95