-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmillis_short_press_long_press_extra.ino
85 lines (77 loc) · 2.36 KB
/
millis_short_press_long_press_extra.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// ---------------------------------------------
// millis_short_press_long_press_extra.ino
// ---------------------------------------------
// License: The Unlicense, Public Domain.
// Author: Koepel
// 2019 february 12
// ---------------------------------------------
//
// Short press and long press a button
// Turn the led on with a short press of the button,
// the led turns on after the button is released.
// Turn the led off with a long press,
// the led turns off while the button is being pressed.
//
// The "State Change Detection" example is used.
// The "Blink without Delay" example is used.
//
// A button 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.
//
// This sketch does the same as millis_short_press_long_press.ino
// But this sketch uses a finite state machine.
enum
{
STATE_IDLE,
STATE_BEFORE_INTERVAL,
STATE_AFTER_INTERVAL,
} state;
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
unsigned long previousMillis;
const unsigned long interval = 2000; // 2 seconds
void setup()
{
pinMode( buttonPin, INPUT_PULLUP);
pinMode( ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
int buttonState = digitalRead( buttonPin);
switch( state)
{
case STATE_IDLE:
// The state is idle.
// Waiting for the button to be pressed.
// The led might be on or off.
if( buttonState == LOW) // button pressed ?
{
state = STATE_BEFORE_INTERVAL; // advance to next state
previousMillis = currentMillis; // remember millis
}
break;
case STATE_BEFORE_INTERVAL:
if( buttonState == HIGH) // button released after short time ?
{
digitalWrite( ledPin, HIGH); // turn led on
state = STATE_IDLE; // returnt idle to wait for button
}
else if( currentMillis - previousMillis >= interval) // button pressed long enough ?
{
digitalWrite( ledPin, LOW); // turn led off
state = STATE_AFTER_INTERVAL; // advance to next state
}
break;
case STATE_AFTER_INTERVAL:
// Wait until button is released to return to idle
if( buttonState == HIGH) // HIGH is not pressed
{
state = STATE_IDLE;
}
break;
}
delay( 10); // a delay as a simple way to debounce the button
}