]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/state_3_one_led/state_3_one_led.ino
multitasking
[sketchbook_andrea] / multitasking / state_3_one_led / state_3_one_led.ino
1 /* Blink con state machine
2
3   Accendere e spegnere un led utilizzando una state machine
4 */
5
6 // These variables store the flash pattern
7 // and the current state of the LED
8
9 int ledPin = 13; // the number of the LED pin
10 int ledState = LOW; // ledState used to set the LED
11 unsigned long previousMillis = 0; // will store last time LED was updated
12 long interval = 1000; // 
13
14 void setup()
15 {
16   // set the digital pin as output:
17   pinMode(ledPin, OUTPUT);
18 }
19
20 void loop()
21 {
22   // check to see if it's time to change the state of the LED
23   unsigned long currentMillis = millis();
24   if((ledState == HIGH) && (currentMillis - previousMillis >= interval))
25   {
26     ledState = !ledState ; // Inverti il LED
27     previousMillis = currentMillis; // Remember the time
28     digitalWrite(ledPin, ledState); // Update the actual LED
29
30   } 
31   else  if((ledState == LOW) && (currentMillis - previousMillis >= interval))
32   {
33     ledState = !ledState ; // Inverti il LED
34     previousMillis = currentMillis; // Remember the time
35     digitalWrite(ledPin, ledState); // Update the actual LED
36   }
37
38
39 }
40
41 /* 
42 1. Modificare il codice in modo che si possa precisare il tempo di HIGH
43    e il tempo di LOW
44 2. Aggiungere un LED che brilli ogni 500ms   
45    */
46