-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmillis_within_millis.ino
67 lines (57 loc) · 2.23 KB
/
millis_within_millis.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
57
58
59
60
61
62
63
64
65
66
67
// ---------------------------------------------
// millis_within_millis.ino
// ---------------------------------------------
// License: The Unlicense, Public Domain.
// 2019 january 23 First version, by Koepel.
// 2019 may 7 Changed name of variables, by Koepel
// ---------------------------------------------
//
// millis within millis
//
// Blink a led for some time, and keep it off for some time.
//
// Because millis is timing based, I prefer a sketch that is "event driven".
// Certain events can become active, and the sketch processes them.
// Therefor the second millis timer is not inside the first
// one, but a bool variable is used to pass on information.
//
// Since the blinking can be turned off at any moment,
// the led is turned off when the blinking stops.
//
// The LED_BUILTIN is the led on the Arduino board,
// any other digital pin can be used.
const int ledPin = LED_BUILTIN; // The digital pin to which a led is connected.
unsigned long previousMillisEnableBlink; // for turning on and off the blinking
unsigned long previousMillisBlink; // for the blinking of the led
// The state for the led is the variable 'blink'.
// That could be an integer that is HIGH or LOW, but I choose to use a bool
// with true and false. There is no reason for that, both are good.
bool enableBlink = false;
bool blink = false; // true (led on) or false (led off)
void setup()
{
pinMode( ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if( currentMillis - previousMillisEnableBlink >= 2000)
{
previousMillisEnableBlink = currentMillis;
enableBlink = !enableBlink; // true becomes false, and false becomes true
if( !enableBlink)
{
digitalWrite( ledPin, LOW); // when blinking stops, be sure the led is off
}
}
// Sometimes the part that does the blinking is placed in a "update" function.
if( enableBlink) // is the blinking enabled ?
{
if( currentMillis - previousMillisBlink >= 150)
{
previousMillisBlink = currentMillis;
digitalWrite( ledPin, blink ? HIGH : LOW); // turn bool variable into HIGH and LOW
blink = !blink; // true becomes false, and false becomes true
}
}
}