-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMomentaryButton.cpp
More file actions
29 lines (28 loc) · 838 Bytes
/
Copy pathMomentaryButton.cpp
File metadata and controls
29 lines (28 loc) · 838 Bytes
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
#include <Arduino.h>
#include "MomentaryButton.h"
#include "Logger.h"
MomentaryButton::MomentaryButton() {}
MomentaryButton::MomentaryButton(int pin, void (*pressedCallback)(void), Print *logger):
_logger(logger)
{
pinMode(pin, INPUT);
_pin = pin;
_pressedCallback = pressedCallback;
_lastButtonState = LOW;
_debounceDuration = 50;
_lastTimeButtonStateChanged = 0;
}
void MomentaryButton::checkButton() {
if (millis() - _lastTimeButtonStateChanged > _debounceDuration) {
byte buttonState = digitalRead(_pin);
if (buttonState != _lastButtonState) {
_lastTimeButtonStateChanged = millis();
_lastButtonState = buttonState;
if (buttonState == HIGH) {
// do an action, for example print on Serial
_logger->println("Button pressed");
_pressedCallback();
}
}
}
}