You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
904 B

// Source: https://forum.arduino.cc/index.php?topic=14479.0
#include "button.h"
Button::Button(byte pin) {
this->pin = pin;
init();
}
void Button::init() {
pinMode(pin, INPUT);
}
byte Button::getState() {
byte event = 0;
buttonVal = digitalRead(pin);
// Button pressed down
if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce)
{
event = 1;
downTime = millis();
ignoreUp = false;
}
// Button released
else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce)
{
if (not ignoreUp) upTime = millis();
}
// Test for hold
if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
// Trigger "normal" hold
event = 2;
ignoreUp = true;
// Trigger "long" hold
if ((millis() - downTime) >= longHoldTime)
{
event = 3;
}
}
buttonLast = buttonVal;
return event;
}