//* Detecting how long a button was pressed (with debouncing) *

#define Tbounce (100) #define Tlong (1000) static uint16_t key_A_Ton = 0; // max value is 0xFFFF static uint16_t key_A_Toff = 0; // max value is 0xFFFF uint8_t cur = getButton(); // assumed: 1 pressed, 0 released if (0!=cur) /* is pressed */ { if (key_A_Toff>0) { /* if needed do things that depend on how long this key was released before it is now pressed */ key_A_Toff=0; } else { if (key_A_Ton<0xFFFF) Ton++; if (key_A_Ton>=Tlong) { do_A_long_press_in_progress(); } } } else /* is not pressed */ { if (key_A_Ton>0) { if (key_A_Ton<Tbounce) ; // nothing - debouncing else { if (key_A_Ton<Tlong) { do_A_short_press_done(); } else { do_A_long_press_done(); } } key_A_Ton=0; } else { if (key_A_Toff<0xFFFF) key_A_Toff++; } }