#include <TM1637.h> // Library for the TM1637 4-digit display
// --- Pins Configuration ---
const int buzzer = 8;
const int CLK = 2;
const int DIO = 3;
const int CLK_2 = 50;
const int DIO_2 = 51;
const int pauseButton = 4;
const int set10Button = 5;
const int set0Button = 6;
const int increaseButton = 7;
const int decreaseButton = 9;
// --- Timer and Display ---
int seconds = 600;
int lastDisplayedSeconds = -1;
bool isPaused = false;
unsigned long previousMillis = 0;
const unsigned long interval = 1000; // 1 second
TM1637 tm(CLK, DIO);
TM1637 tm_2(CLK_2, DIO_2);
void setup() {
// Initialize displays
tm.init();
tm.set(BRIGHT_TYPICAL);
tm_2.init();
tm_2.set(BRIGHT_TYPICAL);
// Pin setup
pinMode(buzzer, OUTPUT);
pinMode(pauseButton, INPUT_PULLUP);
pinMode(set10Button, INPUT_PULLUP);
pinMode(set0Button, INPUT_PULLUP);
pinMode(increaseButton, INPUT_PULLUP);
pinMode(decreaseButton, INPUT_PULLUP);
}
void loop() {
handleButtons();
updateTimer();
updateDisplay();
}
void handleButtons() {
if (digitalRead(pauseButton) == LOW) {
delay(200);
isPaused = !isPaused;
}
if (digitalRead(set10Button) == LOW) {
delay(200);
seconds = 600;
}
if (digitalRead(set0Button) == LOW) {
delay(200);
seconds = 0;
}
if (digitalRead(increaseButton) == LOW) {
delay(200);
seconds += 5;
if (seconds > 600) seconds = 600;
}
if (digitalRead(decreaseButton) == LOW) {
delay(200);
seconds -= 5;
if (seconds < 0) seconds = 0;
}
}
void updateTimer() {
unsigned long currentMillis = millis();
if (!isPaused && (currentMillis - previousMillis >= interval)) {
previousMillis = currentMillis;
if (seconds > 0) {
seconds--;
} else {
// Stop timer and alert
isPaused = true;
tone(buzzer, 1000, 500);
delay(500);
noTone(buzzer);
}
if (seconds == 600) { // Reached 10 minutes
tone(buzzer, 1000, 1000);
}
}
}
void updateDisplay() {
if (seconds != lastDisplayedSeconds) {
lastDisplayedSeconds = seconds;
int minutes = seconds / 60;
int secs = seconds % 60;
tm.display(0, minutes / 10);
tm.display(1, minutes % 10);
tm.display(2, secs / 10);
tm.display(3, secs % 10);
tm_2.display(0, minutes / 10);
tm_2.display(1, minutes % 10);
tm_2.display(2, secs / 10);
tm_2.display(3, secs % 10);
}
}