]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino
Clean up multitasking, bottoni con pooling e interrupts
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_2_led_cleanup / BlinkWithoutDelay_2_led_cleanup.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;           // Not used
33               
34 long previousMillisA = 0;        // will store last time LED was updated
35 unsigned 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     
53     previousMillisA += intervalA ;
54
55     // if the LED is off turn it on and vice-versa:
56      ledStateA = !ledStateA;
57     // set the LED with the ledState of the variable:
58     digitalWrite(ledA, ledStateA);
59   }
60   
61 // Secondo LED: contratta
62     if (millis() - previousMillisB >= intervalB) {
63     digitalWrite(ledB, !digitalRead(ledB));
64     previousMillisB += intervalB ;
65   }
66 }
67
68 /* Domande
69  1. Provare a isolare il codice per accendere ogni singolo led in una funzione:
70     - Quali variabili determinano il comportamento del LED?
71     - Come cambiano durante il corso dello script?
72     - Sono globali o locali? 
73     - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione?
74  */
75
76
77