1 /* Blink con state machine: due led
3 Accendere e spegnere due led utilizzando una state machine
4 specificando sia il tempo accensione che di spegnimento.
8 // These variables store the flash pattern
9 // and the current state of the LED
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; //
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; //
25 // set the digital pin as output:
26 pinMode(ledPinA, OUTPUT);
27 pinMode(ledPinB, OUTPUT);
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))
37 ledStateA = 1 - ledStateA ; // Inverti il LED
38 previousMillisA = currentMillis; // Remember the time
39 digitalWrite(ledPinA, ledStateA); // Update the actual LED
42 else if((ledStateA == LOW) && (currentMillis - previousMillisA >= intervalLowA))
44 ledStateA = 1 - ledStateA ; // Inverti il LED
45 previousMillisA = currentMillis; // Remember the time
46 digitalWrite(ledPinA, ledStateA); // Update the actual LED
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))
55 ledStateB = 1 - ledStateB ; // Inverti il LED
56 previousMillisB = currentMillis; // Remember the time
57 digitalWrite(ledPinB, ledStateB); // Update the actual LED
60 else if((ledStateB == LOW) && (currentMillis - previousMillisB >= intervalLowB))
62 ledStateB = 1 - ledStateB ; // Inverti il LED
63 previousMillisB = currentMillis; // Remember the time
64 digitalWrite(ledPinA, ledStateB); // Update the actual LED
69 1. E' possibile razzinalizzare il codice utilizzando una funzione?
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