#include <TM1637TinyDisplay.h>
#define CLK 3
#define DIO 2
#define BTN 4
TM1637TinyDisplay display(CLK, DIO);
volatile bool running = false;
volatile uint8_t hundredths = 0;
volatile uint8_t seconds = 0;
HardwareTimer *timer = nullptr;
void onTimer() {
if (running) {
hundredths++;
if (hundredths >= 100) {
hundredths = 0;
seconds++;
if (seconds >= 100) seconds = 0;
}
}
}
void setup() {
pinMode(BTN, INPUT_PULLUP);
display.setBrightness(7);
display.showNumberDec(0, true);
timer = new HardwareTimer(TIM1);
timer->setOverflow(10000, MICROSEC_FORMAT); // 10 ms
timer->attachInterrupt(onTimer);
timer->resume();
}
void loop() {
static bool lastBtn = HIGH;
bool btn = digitalRead(BTN);
if (lastBtn == HIGH && btn == LOW) {
running = !running;
delay(200);
}
lastBtn = btn;
int value = seconds * 100 + hundredths;
display.showNumberDec(value, true);
}