]> 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
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 ledA =  13;      // the number of the LED pin
27 int ledB = 12; //Secondo LED
28
29 // Variables will change:
30 int ledStateA = LOW;             // ledState used to set the LED
31 int ledStateB = LOW;             // ledState used to set the LED
32
33 long previousMillisA = 0;        // will store last time LED was updated
34 long previousMillisB = 0;        // will store last time LED was updated
35
36 // the follow variables is a long because the time, measured in miliseconds,
37 // will quickly become a bigger number than can be stored in an int.
38 long intervalA = 1000;           // interval at which to blink (milliseconds)
39 long intervalB = 500;           // interval at which to blink (milliseconds)
40
41 void setup() {
42   // set the digital pin as output:
43   pinMode(ledA, OUTPUT);      
44   pinMode(ledB, OUTPUT);  
45 }
46
47 void loop()
48 {
49   // here is where you'd put code that needs to be running all the time.
50
51   // check to see if it's time to blink the LED; that is, if the 
52   // difference between the current time and last time you blinked 
53   // the LED is bigger than the interval at which you want to 
54   // blink the LED.
55   unsigned long currentMillis = millis();
56
57   if(currentMillis - previousMillisA > intervalA) {
58     // save the last time you blinked the LED 
59     previousMillisA = currentMillis;   
60
61     // if the LED is off turn it on and vice-versa:
62     if (ledStateA == LOW)
63       ledStateA = HIGH;
64     else
65       ledStateA = LOW;
66     // set the LED with the ledState of the variable:
67     digitalWrite(ledA, ledStateA);
68   }
69   
70     if(currentMillis - previousMillisB > intervalB) {
71     // save the last time you blinked the LED 
72     previousMillisB = currentMillis;   
73
74     // if the LED is off turn it on and vice-versa:
75     if (ledStateB == LOW)
76       ledStateB = HIGH;
77     else
78       ledStateB = LOW;
79     // set the LED with the ledState of the variable:
80     digitalWrite(ledB, ledStateB);
81   }
82 }
83
84 /* Domande
85  2. Inserire un secondo LED con intervallo 500ms
86  1. Trasformare il codice utilizzato in una State Machine
87  
88  */
89
90
91