Last active
May 18, 2023 20:07
-
-
Save rahji/546f358d4c3cb7d9d077d4a49ce35fb4 to your computer and use it in GitHub Desktop.
debounce test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef F_CPU | |
#undef F_CPU | |
#define F_CPU 8000000UL | |
#endif | |
#include "buttonDebouncer.h" | |
#include "millis.h" | |
#define NR_OF_BUTTONS 5 | |
uint8_t BUTTONS_LOCATION[NR_OF_BUTTONS] = {'C', 0, 1, 2, 3}; // all buttons on PORTC | |
enum Buttons{ | |
BTN_1 = 1, // PC0 | |
BTN_2 = 2, // PC1 | |
BTN_3 = 4, // PC2 | |
BTN_4 = 8, // PC3 | |
}Buttons; | |
void blinkLED(uint8_t led_pin, uint8_t duration); | |
int main(void){ | |
buttons_t pressed_buttons = 0; | |
buttons_t pushed_buttons = 0; | |
buttons_t button_combination = 0; | |
// set data direction register for PB0 through PB3 (for 4 LEDs) to output | |
DDRB |= 0x0F; | |
// Setup millis | |
millis_init(); | |
// Setup button debouncer | |
debouncerSetup(BUTTONS_LOCATION, NR_OF_BUTTONS); | |
btnSetLongPressTime(2000); | |
// Enable global interrupts | |
sei(); | |
// signal that we're up by flashing of the three LEDs in turn | |
for (uint8_t i = 0; i <= 3; ++i) { | |
blinkLED(i, 100); | |
} | |
millis_reset(); | |
while(1){ | |
// Check if debouncing is complete | |
if(DEBOUNCE_FINISH){ | |
DEBOUNCE_FINISH = 0; | |
pressed_buttons = debouncerPressedButtons(); | |
pushed_buttons = debouncerPushedButtons(); | |
button_combination = debouncerButtonCombination(); | |
// // If a button was long pressed | |
if (BUTTON_LONG_PRESS){ | |
// Check which button | |
pushed_buttons = pressed_buttons; | |
} | |
if (button_combination == (BTN_1 | BTN_3)) { | |
// buttons 1 and 3 were pushed so save configuration to EEPROM (in real program) | |
// for this test, turn off all LEDs on PORTB | |
PORTB &= ~0x0F; | |
} | |
// Check which button was pushed | |
switch (pushed_buttons) { | |
case BTN_1: | |
if (BUTTON_LONG_PRESS) { | |
// This button was long pressed, so turn off LED | |
PORTB &= ~_BV(PB0); // clear PB0 LED pin | |
} else { | |
// This button was pushed but not long pressed, so turn on LED (even if it was already on) | |
PORTB |= _BV(PB0); // set PB0 LED pin | |
} | |
break; | |
case BTN_2: | |
if (BUTTON_LONG_PRESS) { | |
// This button was long pressed, so turn off LED | |
PORTB &= ~_BV(PB1); // clear PB1 LED pin | |
} else { | |
// This button was pushed but not long pressed, so turn on LED (even if it was already on) | |
PORTB |= _BV(PB1); // set PB1 LED pin | |
} | |
break; | |
case BTN_3: | |
if (BUTTON_LONG_PRESS) { | |
// This button was long pressed, so turn off LED | |
PORTB &= ~_BV(PB2); // clear PB2 LED pin | |
} else { | |
// This button was pushed but not long pressed, so turn on LED (even if it was already on) | |
PORTB |= _BV(PB2); // set PB2 LED pin | |
} | |
break; | |
case BTN_4: | |
if (BUTTON_LONG_PRESS) { | |
// This button was long pressed, so turn off LED | |
PORTB &= ~_BV(PB3); // clear PB3 LED pin | |
} else { | |
// This button was pushed but not long pressed, so turn on LED (even if it was already on) | |
PORTB |= _BV(PB3); // set PB3 LED pin | |
} | |
break; | |
} | |
} | |
} | |
} | |
// Millis timer | |
ISR(ISR_VECT){ | |
++milliseconds; | |
// Default: debouncing every 10ms, 1ms timer resolution | |
buttonDebouncerTimer(); // 2.4us @ 8MHz | |
} | |
// function to blink an LED for a number of milliseconds (0-255) | |
void blinkLED(uint8_t led_pin, uint8_t duration) { | |
millis_t start = millis_get(); // start: timestamp | |
PORTB |= _BV(led_pin); // set PORTB LED pin | |
while (millis_get() - start < duration); | |
PORTB &= ~_BV(led_pin); // clear PORTB LED pin | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment