Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.