// Countdown Timer with Relay Control without Countimer Library
#include <LiquidCrystal_I2C.h> // I2C LCD library
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust I2C address if needed
#define bt_set A3
#define bt_up A2
#define bt_down A1
#define bt_start A0
int time_s = 0;
int time_m = 0;
int time_h = 0;
int set = 0;
int flag2 = 0;
int relay = 5;
int buzzer = 6;
unsigned long previousMillis = 0;
const long interval = 1000; // 1-second interval
void setup() {
Serial.begin(9600);
pinMode(bt_set, INPUT_PULLUP);
pinMode(bt_up, INPUT_PULLUP);
pinMode(bt_down, INPUT_PULLUP);
pinMode(bt_start, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Welcome To ");
lcd.setCursor(0, 1);
lcd.print("Countdown Timer");
delay(2000);
lcd.clear();
eeprom_write();
eeprom_read();
}
void print_time() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (flag2 == 1) {
time_s--;
if (time_s < 0) {
time_s = 59;
time_m--;
}
if (time_m < 0) {
time_m = 59;
time_h--;
}
if (time_h < 0) {
time_h = 0;
time_m = 0;
time_s = 0;
flag2 = 0;
// Turn off relay and trigger buzzer
digitalWrite(relay, LOW);
buzzer_alert();
}
}
}
}
void buzzer_alert() {
for (int i = 0; i < 3; i++) {
tone(buzzer, 440);
delay(1000);
noTone(buzzer);
delay(1000);
}
}
void loop() {
print_time();
if (digitalRead(bt_set) == LOW) {
set = (set + 1) % 4;
delay(200);
}
if (digitalRead(bt_up) == LOW) {
if (set == 1) time_s++;
if (set == 2) time_m++;
if (set == 3) time_h++;
if (time_s > 59) time_s = 0;
if (time_m > 59) time_m = 0;
if (time_h > 99) time_h = 0;
if (set > 0) eeprom_write();
delay(200);
}
if (digitalRead(bt_down) == LOW) {
if (set == 1) time_s--;
if (set == 2) time_m--;
if (set == 3) time_h--;
if (time_s < 0) time_s = 59;
if (time_m < 0) time_m = 59;
if (time_h < 0) time_h = 99;
if (set > 0) eeprom_write();
delay(200);
}
if (digitalRead(bt_start) == LOW) {
flag2 = 1;
eeprom_read();
digitalWrite(relay, HIGH);
delay(200);
}
// Update LCD display
lcd.setCursor(0, 0);
if (set == 0) lcd.print(" Timer ");
if (set == 1) lcd.print(" Set Timer SS ");
if (set == 2) lcd.print(" Set Timer MM ");
if (set == 3) lcd.print(" Set Timer HH ");
lcd.setCursor(4, 1);
if (time_h <= 9) lcd.print("0");
lcd.print(time_h);
lcd.print(":");
if (time_m <= 9) lcd.print("0");
lcd.print(time_m);
lcd.print(":");
if (time_s <= 9) lcd.print("0");
lcd.print(time_s);
lcd.print(" ");
delay(50);
}
void eeprom_write() {
EEPROM.write(1, time_s);
EEPROM.write(2, time_m);
EEPROM.write(3, time_h);
}
void eeprom_read() {
time_s = EEPROM.read(1);
time_m = EEPROM.read(2);
time_h = EEPROM.read(3);
}