]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino
175f70567a420a94be0253115318a7f037ba100e
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_6_1_interrupt / BlinkWithoutDelay_6_1_interrupt.ino
1 /* Blink without Delay
2
3  Utilizzo di un interrupt per richiamare update()
4  una volta ogni millesimo di secondo
5  */
6
7 // Oggetti:
8 class Lampeggiatore {
9   // Lampeggia un LED utilizzando millis()
10   // Variabili
11   int ledPin ;           // il numero del LED pin
12   int ledState ;         // stato attuale del LED
13   long interval ;        // milliseconds di intervallo nel lampeggiare
14   long previousMillis ;  //precedente cambio di stato
15
16   // Constructor: come viene instanziato un oggetto facente parte della classe
17 public:
18   Lampeggiatore(int pin, long time)
19   {
20     ledPin = pin;
21     pinMode(ledPin, OUTPUT);
22     ledState = LOW;
23     previousMillis = 0;
24     interval = time;
25   }
26
27 // Una funzione facente parte di una classe prende il nome di "metodo" della stessa:
28   void Update () {
29     // Illumina il ledB secondo un intervallo passato come argomento
30
31     if (millis() > previousMillis + interval) {
32       // save the last time you blinked the LED 
33       previousMillis = millis();   
34
35       // if the LED is off turn it on and vice-versa:
36       ledState = !ledState ; // Inverti il LED
37     }
38     // set the LED with the ledState of the variable:
39     digitalWrite(ledPin, ledState);
40   }
41   
42 };
43
44 // Instanziamo i due led dalla classe 
45 Lampeggiatore ledA(13, 1000);
46 Lampeggiatore ledB(12, 500);
47
48 void setup() {
49   // Interrupt ogni millesimo di secondo
50   // Timer0 is already used for millis() - we'll just interrupt somewhere
51   // in the middle and call the "Compare A" function below
52   OCR0A = 0xAF;
53   TIMSK0 |= _BV(OCIE0A);
54 }
55 // A ogni millisecondo l'interrupt attiva questa funzione
56 SIGNAL(TIMER0_COMPA_vect) 
57
58   ledA.Update();
59   ledB.Update();
60
61 void loop()
62 {
63   // Gli aggiornamenti dei LED ora vengono richiamati dalla funzione
64   // associata agli interrupt
65 //  ledA.Update();
66 //  ledB.Update();
67 }
68
69
70