-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmillis_single_delay.ino
56 lines (49 loc) · 1.38 KB
/
millis_single_delay.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ---------------------------------------------
// millis_single_delay.ino
// ---------------------------------------------
// License: The Unlicense, Public Domain.
// Author: Koepel
// 2019 january 23
// ---------------------------------------------
//
// Single shot software timer with millis().
// Turn on a led for 4 seconds when a switch is used.
//
// The 4 seconds starts when the switch is released.
// When the timing should start at the moment the
// switch is pressed, then the State Change Detection
// should be added.
//
// The switch is connected to pin 2 and GND.
//
// The LED_BUILTIN led is the led on the Arduino board,
// any other digital pin can be used.
//
const int switchPin = 2;
const int ledPin = LED_BUILTIN;
unsigned long previousMillis;
const unsigned long interval = 4000;
bool enabled = false;
void setup()
{
pinMode( switchPin, INPUT_PULLUP);
pinMode( ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if( digitalRead( switchPin) == LOW) // low is active
{
digitalWrite( ledPin, HIGH); // turn on led
previousMillis = currentMillis;
enabled = true; // turn on software timer
}
if( enabled) // software timer is active ?
{
if( currentMillis - previousMillis >= interval)
{
digitalWrite( ledPin, LOW); // turn off led
enabled = false; // stop software timer
}
}
}