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