#include <LiquidCrystal.h>
// Setting up button pins
const int plusButtonPin = 2;
const int minusButtonPin = 3;
const int startStopButtonPin = 5;
const int resetButtonPin = 4;
// Set Up Buzzer Pin
int buzzerPin = 6;
// Button Pressed Variable
int buttonPressed;
// Constants for LCD interface
const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int minutes = 0; // Variable to store the minutes of the timer
int seconds = 0; // Variable to store the seconds of the timer
bool timerRunning = false; // Flag to indicate if the timer is running
// For Debounce on Buttons
unsigned long currentTime = 0; // Millis
unsigned long lastPressTime = 0; // Gets the last time Button is pressed
unsigned long timeSinceLastPress = 0; // Stores the millis - lastTime Press
void setup() {
lcd.begin(16, 2); // Start the LCD
lcd.print("Set Timer: 00:00"); // Display timer value before anything
// Setting Buttons as inputs
pinMode(plusButtonPin, INPUT);
pinMode(minusButtonPin, INPUT);
pinMode(startStopButtonPin, INPUT);
pinMode(resetButtonPin, INPUT);
// Setting Buzzer as an output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Find the time Since it was last pressed (For Debounce)
timeSinceLastPress = millis() - lastPressTime;
// Function to find if the button is pressed and execute a function based on the corresponding button pressed
buttonPressed = checkButtonPress();
// Switch which passes in the button pressed for argument
switch (buttonPressed) {
case plusButtonPin:
addMinutes(); // Function to handle the + Button press
break;
case minusButtonPin:
minusMinutes(); // Function to handle the - Button press
break;
case startStopButtonPin:
if (timerRunning)
stopTimer(); // Function to handle the stop Button press
else
startTimer(); // Function to handle the start Button press
break;
case resetButtonPin:
resetTimer(); // Function to handle the reset Button press
break;
}
// Update the timer if it is running
if (timerRunning) {
updateTimer();
displayTimer();
// Add a 1s delay for accurate countdown
delay(1000);
}
}
// Checks which buttons were pressed and returns the corresponding pin number (With Debounce)
int checkButtonPress() {
if (timeSinceLastPress > 200) {
if (digitalRead(plusButtonPin) == HIGH) {
lastPressTime = millis();
stopButtonFeedback();
return plusButtonPin;
}
if (digitalRead(minusButtonPin) == HIGH) {
lastPressTime = millis();
stopButtonFeedback();
return minusButtonPin;
}
if (digitalRead(startStopButtonPin) == HIGH) {
lastPressTime = millis();
stopButtonFeedback();
return startStopButtonPin;
}
if (digitalRead(resetButtonPin) == HIGH) {
lastPressTime = millis();
stopButtonFeedback();
return resetButtonPin;
}
}
return 0; // If no button is pressed
}
//
//
//Button Functions
//
//
// Function for handling the + button press
void addMinutes() {
if (!timerRunning) {
minutes++;
if (minutes > 99) {
minutes = 99;
}
displayTimer();
}
buttonFeedback();
}
// Function for handling the - button
void minusMinutes() {
if (!timerRunning) {
minutes--;
if (minutes < 0) {
minutes = 0;
}
displayTimer();
}
buttonFeedback();
}
// Function for handling the start button press
void startTimer() {
if (!timerRunning) {
timerRunning = true;
lcd.setCursor(0, 1);
lcd.print("Timer: Running ");
}
buttonFeedback();
}
// Function for the stop button press
void stopTimer() {
if (timerRunning) {
timerRunning = false;
lcd.setCursor(0, 1);
lcd.print("Timer: Stopped ");
}
buttonFeedback();
}
// Function for handling the reset button press
void resetTimer() {
minutes = 0;
seconds = 0;
timerRunning = false;
lcd.setCursor(0, 1);
lcd.print("Timer: Reset ");
displayTimer();
buttonFeedback();
}
//
//
//LCD Display Functions
//
//
// Function to update the timer's time foreach iteration of if(timrruning)
void updateTimer() {
if (seconds > 0) {
seconds--;
} else {
if (minutes > 0) {
minutes--;
seconds = 59;
} else {
timerRunning = false;
lcd.setCursor(0, 1);
lcd.print("Timer: Finished ");
playTone();
return;
}
}
}
// Function to display the timer on the LCD in Min:Sec format
void displayTimer() {
lcd.setCursor(11, 0);
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
}
//
//
//Buzzer Functions
//
//
// Function to play a tone when the timer finishes
void playTone() {
//Bool for playing a repeated sound
bool playBuzz = true;
while (playBuzz) {
tone(buzzerPin, 1000, 500);
delay(500); // Delay to create a beeping sound
// Check if the reset is pressed
if (digitalRead(resetButtonPin) == HIGH) {
playBuzz = false; //When Reset button pressed, exit the loop
}
}
noTone(buzzerPin); // Stop the annoying beep
}
// Function to stop the tone
void stopTone() {
noTone(buzzerPin);
}
// Function to provide button feedback with a short burst sounf
void buttonFeedback() {
tone(buzzerPin, 1000, 5);
}
// Function to stop the button feedback
void stopButtonFeedback() {
noTone(buzzerPin);
}