or timers
you'll have to implement the setup, enable and disable of the timer.
#include <Arduino.h>
const uint8_t btn_pin = 2;
volatile uint8_t btn_flag;
volatile uint16_t btn_cntr;
const uint16_t btn_max = 10000;
const uint8_t btn_decr = 50;
void setup() {
Serial.begin(9600);
DDRB = 0; //set port b as input
setup_T0();
btn_flg = 0;
flag_btn = 0;
btn_cntr = btn_max;
}
void loop() {
// wait till pin goes high
while (!PINB2) {}
enable_T0()
// wait till pin goes low
while (PINB2) {
if (btn_flag) {
btn_flag = 0;
display(btn_cntr)
}
if (btn_cntr > btn_max) {
// btn_cntr has gone from 10000 to smaller than 0
}
}
disable_T0();
}
// interrupt handler of T0, change x
ISR(TIMER0_COMPx_vect) {
btn_cntr -= btn_decr;
btn_flag = 1;
}
And according to attachInterrupt() - Arduino Reference you can attach an interrupt handler to 2 pins (i assume you use an UNO). Than you can omit the 2 while loops and just check for flag in the main loop.
You could even use the 2 compare registers of the timer and change bit of code, so everything happens in the handlers and the main loop keeps updating the display