#include <TimerOne.h>
// Pinout
const int digitPins[] = {2, 3, 4, 5}; // Arduino pins connected to 7-segment display digit pins
const int segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; // Arduino pins connected to 7-segment display segment pins
const int buttonPin = A0; // Arduino pin connected to the push button
// Timer settings
const int timerDuration = 60; // Countdown duration in seconds
// Global variables
int remainingTime = 0;
bool timerRunning = false;
void setup() {
// Initialize pins
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
// Set initial time
remainingTime = timerDuration;
// Configure Timer1 interrupt
Timer1.initialize(1000000); // Set timer interval to 1 second
Timer1.attachInterrupt(timerInterrupt);
}
void loop() {
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
if (!timerRunning) {
startTimer();
} else {
stopTimer();
}
delay(200); // Debounce delay
}
}
void startTimer() {
timerRunning = true;
}
void stopTimer() {
timerRunning = false;
}
void timerInterrupt() {
if (timerRunning) {
remainingTime--;
if (remainingTime <= 0) {
stopTimer();
}
}
updateDisplay();
}
void updateDisplay() {
int minutes = remainingTime / 60;
int seconds = remainingTime % 60;
// Display minutes
displayDigit(0, minutes / 10);
displayDigit(1, minutes % 10);
// Display seconds
displayDigit(2, seconds / 10);
displayDigit(3, seconds % 10);
}
void displayDigit(int digit, int value) {
// Clear all digits
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], HIGH);
}
// Set the selected digit
digitalWrite(digitPins[digit], LOW);
// Set the segments to display the value
switch (value) {
case 0:
digitalWrite(segmentPins[0], LOW);
digitalWrite(segmentPins[1], LOW);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], LOW);
digitalWrite(segmentPins[4], LOW);
digitalWrite(segmentPins[5], LOW);
digitalWrite(segmentPins[6], HIGH);
break;
case 1:
digitalWrite(segmentPins[0], HIGH);
digitalWrite(segmentPins[1], LOW);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], HIGH);
digitalWrite(segmentPins[4], HIGH);
digitalWrite(segmentPins[5], HIGH);
digitalWrite(segmentPins[6], HIGH);
break;
case 2:
digitalWrite(segmentPins[0], LOW);
digitalWrite(segmentPins[1], LOW);
digitalWrite(segmentPins[2], HIGH);
digitalWrite(segmentPins[3], LOW);
digitalWrite(segmentPins[4], LOW);
digitalWrite(segmentPins[5], HIGH);
digitalWrite(segmentPins[6], LOW);
break;
case 3:
digitalWrite(segmentPins[0], LOW);
digitalWrite(segmentPins[1], LOW);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], LOW);
digitalWrite(segmentPins[4], HIGH);
digitalWrite(segmentPins[5], HIGH);
digitalWrite(segmentPins[6], LOW);
break;
case 4:
digitalWrite(segmentPins[0], HIGH);
digitalWrite(segmentPins[1], LOW);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], HIGH);
digitalWrite(segmentPins[4], HIGH);
digitalWrite(segmentPins[5], LOW);
digitalWrite(segmentPins[6], LOW);
break;
case 5:
digitalWrite(segmentPins[0], LOW);
digitalWrite(segmentPins[1], HIGH);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], LOW);
digitalWrite(segmentPins[4], HIGH);
digitalWrite(segmentPins[5], LOW);
digitalWrite(segmentPins[6], LOW);
break;
case 6:
digitalWrite(segmentPins[0], LOW);
digitalWrite(segmentPins[1], HIGH);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], LOW);
digitalWrite(segmentPins[4], LOW);
digitalWrite(segmentPins[5], LOW);
digitalWrite(segmentPins[6], LOW);
break;
case 7:
digitalWrite(segmentPins[0], LOW);
digitalWrite(segmentPins[1], LOW);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], HIGH);
digitalWrite(segmentPins[4], HIGH);
digitalWrite(segmentPins[5], HIGH);
digitalWrite(segmentPins[6], HIGH);
break;
case 8:
digitalWrite(segmentPins[0], LOW);
digitalWrite(segmentPins[1], LOW);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], LOW);
digitalWrite(segmentPins[4], LOW);
digitalWrite(segmentPins[5], LOW);
digitalWrite(segmentPins[6], LOW);
break;
case 9:
digitalWrite(segmentPins[0], LOW);
digitalWrite(segmentPins[1], LOW);
digitalWrite(segmentPins[2], LOW);
digitalWrite(segmentPins[3], LOW);
digitalWrite(segmentPins[4], HIGH);
digitalWrite(segmentPins[5], LOW);
digitalWrite(segmentPins[6], LOW);
break;
}
}