#include <TM1637.h> // Include the library for the TM1637 4-digit display
int buzzer = 8;
const int CLK = 2; // Set the clock pin of the TM1637 to Arduino pin 2
const int DIO = 3; // Set the data pin of the TM1637 to Arduino pin 3
const int pauseButton = 4; // Button to pause/resume the countdown
const int set10Button = 5; // Button to set time to 10 minutes
const int set0Button = 6; // Button to set time to 0
const int increaseButton = 7; // Button to increase time
const int decreaseButton = 9; // Button to decrease time
bool isPaused = false; // State variable to check if paused
TM1637 tm(CLK, DIO); // Create a display object named 'tm' using the defined pins
int seconds = 600; // Initialize a variable to keep track of the number of seconds passed
int val = -1;
void setup() {
tm.init(); // Initialize the display
pinMode(buzzer, OUTPUT); // Buzzer output
tm.set(BRIGHT_TYPICAL); // Set the brightness to a typical (default) level
// Set buttons as input pullups
pinMode(pauseButton, INPUT_PULLUP);
pinMode(set10Button, INPUT_PULLUP);
pinMode(set0Button, INPUT_PULLUP);
pinMode(increaseButton, INPUT_PULLUP);
pinMode(decreaseButton, INPUT_PULLUP);
}
void loop() {
// --- Button handling ---
if (digitalRead(pauseButton) == LOW)
{
delay(200); // Simple debounce
isPaused = !isPaused; // Toggle pause/resume
}
if (digitalRead(set10Button) == LOW) {
delay(200);
seconds = 600; // Set timer to 10 minutes
}
if (digitalRead(set0Button) == LOW)
{
delay(200);
seconds = 0; // Set timer to 0
}
if (digitalRead(increaseButton) == LOW)
{
delay(200);
seconds = seconds +5; // Increase timer by 1 seconds
if (seconds > 600) seconds = 600; //Prevent the time to go above 600 seconds
}
if (digitalRead(decreaseButton) == LOW)
{
delay(200);
seconds = seconds - 5; // Decrease timer by 1 minute
if (seconds < 0) seconds = 0; // Prevent negative time
}
// --- Your original countdown code ---
if (seconds > val)
{
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);
if (minutes == 10) {
tone(buzzer, 1000, 1000);
}
if (!isPaused) {
delay(100); // Wait for 10 seconds (your original delay)
seconds--; // Decrease time only if not paused
}
}
else
{
seconds = seconds + 600;
delay(1000);
}
}