]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/state_5_two_led_two_timers/state_5_two_led_two_timers.ino
multitasking
[sketchbook_andrea] / multitasking / state_5_two_led_two_timers / state_5_two_led_two_timers.ino
1 /* Blink con state machine: due led
2  
3  Accendere e spegnere due led utilizzando una state machine
4  specificando sia il tempo accensione che di spegnimento.
5  
6  */
7
8 // These variables store the flash pattern
9 // and the current state of the LED
10
11 int ledPinA = 13; // the number of the LED pin
12 int ledStateA = LOW; // ledState used to set the LED
13 unsigned long previousMillisA = 0; // will store last time LED was updated
14 long intervalHighA = 1000; // 
15 long intervalLowA = 200; // 
16
17 int ledPinB = 12; // the number of the LED pin
18 int ledStateB = LOW; // ledState used to set the LED
19 unsigned long previousMillisB = 0; // will store last time LED was updated
20 long intervalHighB = 500; // 
21 long intervalLowB = 100; // 
22
23 void setup()
24 {
25   // set the digital pin as output:
26   pinMode(ledPinA, OUTPUT);
27   pinMode(ledPinB, OUTPUT);
28 }
29
30 void loop()
31 {
32   // Primo LED
33   // check to see if it's time to change the state of the LED
34   unsigned long currentMillis = millis();
35   if((ledStateA == HIGH) && (currentMillis - previousMillisA >= intervalHighA))
36   {
37     ledStateA = 1 - ledStateA ; // Inverti il LED
38     previousMillisA = currentMillis; // Remember the time
39     digitalWrite(ledPinA, ledStateA); // Update the actual LED
40
41   } 
42   else  if((ledStateA == LOW) && (currentMillis - previousMillisA >= intervalLowA))
43   {
44     ledStateA = 1 - ledStateA ; // Inverti il LED
45     previousMillisA = currentMillis; // Remember the time
46     digitalWrite(ledPinA, ledStateA); // Update the actual LED
47   }
48
49 // Secondo LED
50
51   // check to see if it's time to change the state of the LED
52   unsigned long currentMillis = millis();
53   if((ledStateB == HIGH) && (currentMillis - previousMillisB >= intervalHighB))
54   {
55     ledStateB = 1 - ledStateB ; // Inverti il LED
56     previousMillisB = currentMillis; // Remember the time
57     digitalWrite(ledPinB, ledStateB); // Update the actual LED
58
59   } 
60   else  if((ledStateB == LOW) && (currentMillis - previousMillisB >= intervalLowB))
61   {
62     ledStateB = 1 - ledStateB ; // Inverti il LED
63     previousMillisB = currentMillis; // Remember the time
64     digitalWrite(ledPinA, ledStateB); // Update the actual LED
65   }
66 }
67
68 /* Domande
69  1. E' possibile razzinalizzare il codice utilizzando una funzione?
70  
71  
72  Link: 
73  - Utilizzare codice a oggetti per ottimizzare il codice: 
74    https://learn.adafruit.com/multi-tasking-the-arduino-part-1?view=all#a-classy-solution
75  */
76
77