]> git.piffa.net Git - arduino/blob - books/ArduinoNextSteps-master/ArduinoNextSteps/sketch_14_02_flashing_2/sketch_14_02_flashing_2.ino
first commit
[arduino] / books / ArduinoNextSteps-master / ArduinoNextSteps / sketch_14_02_flashing_2 / sketch_14_02_flashing_2.ino
1 // sketch_14_02_flashing_2
2
3 const int ledPin = 13;
4 const int switchPin = 5;
5 const int period = 1000;
6
7 boolean flashing = false;
8 long lastChangeTime = 0;
9 int ledState = LOW;
10
11 void setup()
12 {
13   pinMode(ledPin, OUTPUT);
14   pinMode(switchPin, INPUT_PULLUP); 
15 }
16
17 void loop()
18 {
19   if (digitalRead(switchPin) == LOW)
20   {
21     flashing = ! flashing;
22     // and turn the LED off
23     if (! flashing)
24     {
25       digitalWrite(ledPin, LOW);
26     }
27   }
28   long now = millis();
29   if (flashing && now > lastChangeTime + period)
30   {
31     ledState = ! ledState;
32     digitalWrite(ledPin, ledState);
33     lastChangeTime = now;
34   }    
35 }