#include <TM1637.h>
const int CLK = 2;
const int DIO = 3;
const int btnPin = 5;
TM1637 tm(CLK, DIO);
void setup() {
tm.init();
tm.set(BRIGHT_TYPICAL);
pinMode(btnPin, INPUT_PULLUP);
}
unsigned int counter = 0;
bool btnStatePrev = HIGH; // Previous button state
void loop() {
tm.display(0, (counter / 1000) % 10);
tm.display(1, (counter / 100) % 10);
tm.display(2, (counter / 10) % 10);
tm.display(3, counter % 10);
// Read the button state
bool btnState = digitalRead(btnPin);
// Check for a rising edge (button press)
if (btnState == LOW && btnStatePrev == HIGH) {
counter++;
// If the counter reaches 10,000, reset it
if (counter == 10000) {
counter = 0;
}
// Debounce delay to avoid multiple counts from one press
delay(200);
}
// Update the previous button state
btnStatePrev = btnState;
// Add a small delay to avoid rapid counting
delay(100);
}