/* Blink program Latch and Debounce for the MidTiny board (ATtiny1614). Author: Harley Lara Create: 03 May 2021 License: (CC BY-SA 4.0) Attribution-ShareAlike 4.0 International This work may be reproduced, modified, distributed, performed, and displayed for any purpose, but must acknowledge this project. Copyright is retained and must be preserved. The work is provided as is; no warranty is provided, and users accept all liability. */ #define F_CPU 3333333 #include #include #define LED PIN5_bm // Define LED pin #define BUTTON PIN4_bm // Define BUTTON pin // saves the latest status // false: released // true: pressed bool last = false; int push_level = 0; int release_level = 0; int threshold = 50; int main() { PORTA.DIR |= LED; // Define the LED port as output PORTA.OUT &= ~LED; // Set the initial state of the LED OFF PORTA.DIR &= ~BUTTON; // Define the button port as input PORTA.PIN4CTRL = 0; // Don't use the internal pullup resistor while (true) { if (PORTA.IN & BUTTON) { push_level ++; release_level = 0; if (push_level > threshold ){ if (last == false){ PORTA.OUT ^= LED; // LED toggle ON/OFF last = true; } push_level = 0; } } else { release_level ++; push_level = 0; if (release_level > threshold){ last = false; release_level = 0; } } _delay_ms(10); } return 0; }