Skip to content

Instantly share code, notes, and snippets.

@rlogiacco
Last active October 2, 2019 19:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlogiacco/75fea22fd21e97e3466d to your computer and use it in GitHub Desktop.
Save rlogiacco/75fea22fd21e97e3466d to your computer and use it in GitHub Desktop.
Debounce a button
#define DEBOUNCE 15
#define DMASK ((uint16_t)(1<<DEBOUNCE)-1)
#define DF (1<<(uint16_t)(DEBOUNCE-1))
#define DR (DMASK-DF)
// macro for detection of raising edge and debouncing
#define DRE(signal, state) ((state=((state<<1)|(signal&1))&DMASK)==DR)
// macro for detection of falling edge and debouncing
#define DFE(signal, state) ((state=((state<<1)|(signal&1))&DMASK)==DF)
#define BUTTON_PIN 7
uint16_t pinStatus;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
if(DFE(digitalRead(BUTTON_PIN), pinStatus)) {
// do stuff
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment