- Edited
i needed a zero latency (on button press) debounce approach for some buttons on bela, and wrote this. works great! (excerpt):
//buttons are connected with a pull up resistor, so they read HIGH when not pressed
//variables
#define DEBOUNCE_TIME 40 //how long the button has to be "off" to be considered off
bool cState_b0 = 0;
bool pState_b0 = 1;
int debounce_b0 = 0;
//initalize button pin as INPUT in setup
pinMode(context,0,4, INPUT); //Button 1
//in render
for(unsigned int n = 0; n < context->digitalFrames; ++n){
cState_b0 = digitalRead(context, n, 4);
}
if (!cState_b0 && pState_b0) {
//button is pressed down
}
if (cState_b0 && !pState_b0) {
if (debounce_b0 < DEBOUNCE_TIME) {
debounce_b0++;
cState_b0 = 0;
} else debounce_b0 = 0;
}
pState_b0 = cState_b0;