#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address, columns, rows
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
bool countdownRunning = false;
unsigned long countdownStartTime;
unsigned long countdownDuration = 600000;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Countdown Timer");
lcd.setCursor(0, 1);
lcd.print("Press * to start");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*') {
if (!countdownRunning) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown Running");
countdownStartTime = millis();
countdownRunning = true;
} else {
// Stop the countdown if it's running
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown Stopped");
countdownRunning = false;
}
}
// Handle other keys if needed
// ...
}
if (countdownRunning) {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - countdownStartTime;
unsigned long remainingTime = countdownDuration - elapsedTime;
int hours = remainingTime / 3600000;
int minutes = (remainingTime % 3600000) / 60000;
int seconds = ((remainingTime % 3600000) % 60000) / 1000;
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(hours);
lcd.print(":");
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds);
if (remainingTime <= 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown Complete");
countdownRunning = false;
}
}
}