#include <TM1637Display.h>
#include <EEPROM.h>
#define CLK_PIN 2
#define DIO_PIN 3
#define BUTTON_PIN 4
#define ELAPSED_TIME_MIN 8000
#define ELAPSED_TIME_MAX 10099
#define EEPROM_ADDRESS 0
TM1637Display tm1637(CLK_PIN, DIO_PIN);
bool isTimerRunning = false;
bool isButtonPressed = false;
unsigned long startTime;
unsigned long elapsedTime;
int stopCount = 0;
void setup() {
Serial.begin(9600);
Serial.println("Boot Up");
tm1637.setBrightness(7); // Set the brightness (0-7)
pinMode(BUTTON_PIN, INPUT_PULLUP);
stopTimer();
displayStopCount();
delay(3000); // Display the stop count for 3 seconds before starting the game
tm1637.clear(); // Clear the TM1637 display
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
if (!isButtonPressed) {
delay(50);
isButtonPressed = true;
}
} else {
if (isButtonPressed) {
delay(50);
isButtonPressed = false;
if (isTimerRunning) {
stopTimer();
} else {
startTimer();
}
}
}
if (isTimerRunning) {
updateTimerDisplay();
}
}
void startTimer() {
tm1637.clear(); // Clear the TM1637 display
isTimerRunning = true;
startTime = millis();
}
void stopTimer() {
isTimerRunning = false;
elapsedTime = millis() - startTime;
if (elapsedTime >= ELAPSED_TIME_MIN && elapsedTime <= ELAPSED_TIME_MAX) {
stopCount++;
EEPROM.write(EEPROM_ADDRESS, stopCount);
}
}
void updateTimerDisplay() {
elapsedTime = millis() - startTime;
int seconds = elapsedTime / 1000;
int milliseconds = elapsedTime % 1000;
tm1637.showNumberDecEx(seconds * 100 + milliseconds / 10, 0b01100000, true); // Display the time on the TM1637 display (in seconds and milliseconds)
Serial.print(seconds * 100 + milliseconds / 10);
Serial.print(" -");
Serial.println(elapsedTime);
}
void displayStopCount() {
stopCount = EEPROM.read(EEPROM_ADDRESS);
tm1637.showNumberDec(stopCount); // Display the stop count on the TM1637 display
}