#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Countdown Timer with LCD Output in Minutes and Seconds, LED indicator (Custom Buttons for Start, Reset, Increase, Decrease)
const int startButtonPin = 12; // Pin for the start button input
const int resetButtonPin = 13; // Pin for the reset button input
const int increaseButtonPin = 19; // Pin for the increase button input
const int decreaseButtonPin = 18; // Pin for the decrease button input
const int ledPin = 17; // Pin for the LED indicator
unsigned long startTime;
unsigned long countdownDuration = 5 * 60 * 1000; // 5 minutes in milliseconds
unsigned long lastUpdateTime = 0;
unsigned long updateInterval = 1000; // Update interval in milliseconds
bool timerActive = false;
// Variables for button debouncing
unsigned long debounceDelay = 50; // Debounce time in milliseconds
unsigned long lastStartButtonState = HIGH;
unsigned long lastResetButtonState = HIGH;
unsigned long lastIncreaseButtonState = HIGH;
unsigned long lastDecreaseButtonState = HIGH;
unsigned long startButtonState;
unsigned long resetButtonState;
unsigned long increaseButtonState;
unsigned long decreaseButtonState;
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD address to 0x27 for a 20 chars and 4 line display
void setup() {
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(increaseButtonPin, INPUT_PULLUP);
pinMode(decreaseButtonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("----Robots Live-----");
lcd.setCursor(0, 2);
lcd.print("Battle Controller v2");
lcd.setCursor(0, 3);
lcd.print("--------------------");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press start button");
lcd.setCursor(0, 1);
lcd.print("to begin timer");
Serial.begin(9600);
}
void loop() {
// Read the state of the start button
startButtonState = digitalRead(startButtonPin);
// Check if the start button has been pressed and debounced
if (startButtonState != lastStartButtonState) {
delay(debounceDelay);
if (startButtonState != lastStartButtonState) {
if (startButtonState == LOW) {
startTimer();
}
}
}
// Read the state of the reset button
resetButtonState = digitalRead(resetButtonPin);
// Check if the reset button has been pressed and debounced
if (resetButtonState != lastResetButtonState) {
delay(debounceDelay);
if (resetButtonState != lastResetButtonState) {
if (resetButtonState == LOW) {
resetTimer();
}
}
}
// Read the state of the increase button
increaseButtonState = digitalRead(increaseButtonPin);
// Check if the increase button has been pressed and debounced
if (increaseButtonState != lastIncreaseButtonState) {
delay(debounceDelay);
if (increaseButtonState != lastIncreaseButtonState) {
if (increaseButtonState == LOW) {
increaseTime();
}
}
}
// Read the state of the decrease button
decreaseButtonState = digitalRead(decreaseButtonPin);
// Check if the decrease button has been pressed and debounced
if (decreaseButtonState != lastDecreaseButtonState) {
delay(debounceDelay);
if (decreaseButtonState != lastDecreaseButtonState) {
if (decreaseButtonState == LOW) {
decreaseTime();
}
}
}
// Continue updating the timer if it's active
if (timerActive) {
updateTimer();
}
// Update LED indicator
digitalWrite(ledPin, timerActive ? HIGH : LOW);
// Update the last button states
lastStartButtonState = startButtonState;
lastResetButtonState = resetButtonState;
lastIncreaseButtonState = increaseButtonState;
lastDecreaseButtonState = decreaseButtonState;
}
void startTimer() {
Serial.println("Countdown Timer Started");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown started");
startTime = millis(); // Record the start time
timerActive = true;
}
void updateTimer() {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;
if (elapsedTime >= countdownDuration) {
// Countdown has reached 0, indicate completion
Serial.println("Countdown Complete");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown complete");
timerActive = false;
}
// Check if it's time to update
if (currentTime - lastUpdateTime >= updateInterval) {
lastUpdateTime = currentTime;
unsigned long remainingTime = countdownDuration - elapsedTime;
unsigned long remainingMinutes = remainingTime / (60 * 1000);
unsigned long remainingSeconds = (remainingTime % (60 * 1000)) / 1000;
// Output remaining time to LCD
lcd.setCursor(0, 2);
lcd.print("Time left: ");
lcd.print(remainingMinutes);
lcd.print("m ");
lcd.print(remainingSeconds);
lcd.print("s ");
}
}
void resetTimer() {
Serial.println("Timer Reset");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timer reset");
while (digitalRead(resetButtonPin) == LOW) {
// Wait for the reset button to be released
}
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("Press start button");
lcd.setCursor(0, 1);
lcd.print("to begin timer");
timerActive = false;
}
void increaseTime() {
countdownDuration += 10 * 1000; // Increase by 10 seconds
Serial.print("Increased time to: ");
Serial.print(countdownDuration / (60 * 1000));
Serial.print(" minutes and ");
Serial.print((countdownDuration % (60 * 1000)) / 1000);
Serial.println(" seconds");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Increased time to: ");
lcd.print(countdownDuration / (60 * 1000));
lcd.print("m ");
lcd.print((countdownDuration % (60 * 1000)) / 1000);
lcd.print("s ");
}
void decreaseTime() {
if (countdownDuration > 10 * 1000) {
countdownDuration -= 10 * 1000; // Decrease by 10 seconds
Serial.print("Decreased time to: ");
Serial.print(countdownDuration / (60 * 1000));
Serial.print(" minutes and ");
Serial.print((countdownDuration % (60 * 1000)) / 1000);
Serial.println(" seconds");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Decreased time to: ");
lcd.print(countdownDuration / (60 * 1000));
lcd.print("m ");
lcd.print((countdownDuration % (60 * 1000)) / 1000);
lcd.print("s ");
}
}