]> git.piffa.net Git - arduino/blob - books/ArduinoNextSteps-master/ArduinoNextSteps/sketch_14_03_flashing_3/sketch_14_03_flashing_3.ino
giovedi
[arduino] / books / ArduinoNextSteps-master / ArduinoNextSteps / sketch_14_03_flashing_3 / sketch_14_03_flashing_3.ino
1 // sketch_14_03_flashing_3
2
3 #include <Timer.h>
4
5
6 const int ledPin = 13;
7 const int switchPin = 5;
8 const int period = 1000;
9
10 boolean flashing = false;
11 int ledState = LOW;
12 Timer t;
13
14 void setup()
15 {
16   pinMode(ledPin, OUTPUT);
17   pinMode(switchPin, INPUT_PULLUP); 
18   t.every(period, flashIfRequired);
19 }
20
21 void loop()
22 {
23   if (digitalRead(switchPin) == LOW)
24   {
25     flashing = ! flashing;
26     if (! flashing)
27     {
28       digitalWrite(ledPin, LOW);
29     }
30   }
31   t.update();
32 }
33
34 void flashIfRequired()
35 {
36   if (flashing)
37   {
38     ledState = ! ledState;
39     digitalWrite(ledPin, ledState);
40   }   
41 }