Skip to content

Instantly share code, notes, and snippets.

@bananu7
Created March 21, 2019 17:53
Show Gist options
  • Select an option

  • Save bananu7/f4dce3b53eae0046a42682ea1564893c to your computer and use it in GitHub Desktop.

Select an option

Save bananu7/f4dce3b53eae0046a42682ea1564893c to your computer and use it in GitHub Desktop.
A simple snippet for button press filtering
template<int pin>
class Button {
bool state = false;
int presses;
public:
int poll() {
int result = presses > 0;
presses = 0;
return result;
}
void update() {
bool newState = !digitalRead(pin);
// detect rising edge
if (newState && !state) {
presses += 1;
}
state = newState;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment