+void start_debounce_timer() // this breaks millis() function, but who cares
+{
+ debounce_cnt = DEBOUNCE_DELAY;
+
+ TCCR0A = 0; // no prescaler, WGM = 0 (normal)
+ TCCR0B = 1<<CS00; //
+ OCR0A = 255; // 1+255 = 256 -> 16us @ 16 MHz
+ TCNT0 = 0; // reseting timer
+ TIMSK0 = 1<<OCF0A; // enable Interrupt
+
+}
+
+void stop_debounce_timer()
+{
+ // timer0
+ TCCR0B = 0; // no clock source
+ TIMSK0 = 0; // disable timer interrupt
+}
+
+ISR(TIMER0_COMPA_vect)
+{
+ if(((debounce_state & DEBOUNCE_OPEN) && manual_open_pressed()) ||
+ ((debounce_state & DEBOUNCE_CLOSE) && manual_close_pressed())) {
+ if(debounce_cnt) {
+ debounce_cnt--;
+ return;
+ }
+ debounce_state |= DEBOUNCE_FINISHED;
+ }
+ debounce_cnt = DEBOUNCE_DELAY;
+}
+
+boolean manual_open()
+{
+ if(manual_open_pressed()) {
+ if(debounce_state & DEBOUNCE_CLOSE) {
+ stop_debounce_timer();
+ debounce_state = DEBOUNCE_IDLE;
+ return false;
+ }
+
+ if(debounce_state == DEBOUNCE_IDLE) {
+ debounce_state = DEBOUNCE_OPEN;
+ start_debounce_timer();
+ }
+ else if(debounce_state & DEBOUNCE_FINISHED) {
+ stop_debounce_timer();
+ debounce_state = DEBOUNCE_IDLE;
+ return true;
+ }
+ }
+ else if(debounce_state & DEBOUNCE_OPEN) {
+ stop_debounce_timer();
+ debounce_state = DEBOUNCE_IDLE;
+ }
+
+ return false;
+}
+
+boolean manual_close()
+{
+ if(manual_close_pressed()) {
+ if(debounce_state & DEBOUNCE_OPEN) {
+ stop_debounce_timer();
+ debounce_state = DEBOUNCE_IDLE;
+ return false;
+ }
+
+ if(debounce_state == DEBOUNCE_IDLE) {
+ debounce_state = DEBOUNCE_CLOSE;
+ start_debounce_timer();
+ }
+ else if(debounce_state & DEBOUNCE_FINISHED) {
+ stop_debounce_timer();
+ debounce_state = DEBOUNCE_IDLE;
+ return true;
+ }
+ }
+ else if(debounce_state & DEBOUNCE_CLOSE) {
+ stop_debounce_timer();
+ debounce_state = DEBOUNCE_IDLE;
+ }
+
+ return false;
+}
+