#include <TM1637.h>
const int CLK = 2;
const int DIO = 3;
const int buttonPinStart = 4;
const int buttonPinStop = 5;
const int buttonReset = 6;
int lastButtonState;
int lastTimeButtonStateChanged = 0;
unsigned long lastDebounceTime = 0;
unsigned int counter = 0;
int buttonState;
int sw = 0;
long time;
int StateReset = LOW;
TM1637 tm(CLK, DIO);
void setup() {
tm.init();
tm.set(BRIGHT_TYPICAL);
tm.point(1);
pinMode(buttonPinStart, INPUT);
pinMode(buttonPinStop, INPUT);
pinMode(buttonReset, INPUT);
}
void debounceStart(int pin) {
byte buttonState = digitalRead(pin);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
if (buttonState == HIGH) {
if (sw == 0) {
sw = 1;
time = millis();
}
}
}
}
void debounceStop(int pin) {
byte buttonState = digitalRead(pin);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
if (buttonState == HIGH) {
if (sw == 1) {
sw = 0;
}
}
}
}
void debounceReset(int pin) {
byte StateReset = digitalRead(pin);
if (StateReset == HIGH) {
Serial.println("Resetted");
return;
}
}
void loop() {
debounceStart(buttonPinStart);
debounceStop(buttonPinStop);
debounceReset(buttonReset);
tm.display(0, (counter / (10 * 10 * 10)) % 10);
tm.display(1, (counter / (10 * 10)) % 10);
tm.display(2, (counter / 10) % 10);
tm.display(3, counter % 10);
if (sw == 1) {
if ((millis() - time) >= 10) {
counter++;
time = millis();
}
}
if (counter == 10 * 10 * 10 * 10) {
counter = 0;
}
}