#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
#define BUTTON_PIN 4
TM1637Display display(CLK, DIO);
int count = 1;
int buttonState = 0;
int lastButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
display.setBrightness(0x0f); // Set the brightness of the display
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(BUTTON_PIN);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the count if the new button state is HIGH
if (buttonState == HIGH) {
count++;
if (count > 99) {
count = 1;
}
display.showNumberDec(count, false);
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}