-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToggleButton.cpp
More file actions
49 lines (41 loc) · 1.06 KB
/
ToggleButton.cpp
File metadata and controls
49 lines (41 loc) · 1.06 KB
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
// ToggleButton library for Arduino
// By Jester <jester@jestah.net>
// Provides a simple means of using a pushbutton as a toggle switch
#include "ToggleButton.h"
// constructor
// Set up input pin, active state of the button (HIGH or LOW),
// the debounce duration, and the starting toggle state
ToggleButton::ToggleButton(int buttonPin, int buttonActiveState, bool beginState)
{
button = buttonPin;
debounce = 250;
currentState = beginState;
activeState = buttonActiveState;
pinMode(button, INPUT);
};
// Check if the button is active (pressed) and flip the toggle if it is
void ToggleButton::checkButton(void)
{
int state = digitalRead(button);
if(state == activeState)
{
flipState();
}
}
// Flip the toggle state
void ToggleButton::flipState(void)
{
delay(debounce); // Debounce (stablilize) the input
if(currentState == true)
{
currentState = false;
}
else
{
currentState = true;
}
}
void ToggleButton::setDebounce(int duration)
{
debounce = duration;
}