#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int setButtonPin = 8;
int leftButtonPin = 10;
int rightButtonPin = 11;
int upButtonPin = 12;
int downButtonPin = 13;
int alarmPin = 9;
bool alarmActive = false;
int clockHour = 0;
int clockMinute = 0;
int clockSecond = 0;
int alarmHour = 0;
int alarmMinute = 0;
unsigned long previousMillis = 0;
const long interval = 1000;
enum Mode { NORMAL, CLOCK_SET, ALARM_SET };
Mode currentMode = NORMAL;
int cursorPosition = 0;
unsigned long buttonPreviousMillis = 0;
const long buttonInterval = 200;
void setup() {
lcd.begin(16, 2);
lcd.print("Clock and Alarm");
// No delay here
lcd.clear();
pinMode(setButtonPin, INPUT_PULLUP);
pinMode(leftButtonPin, INPUT_PULLUP);
pinMode(rightButtonPin, INPUT_PULLUP);
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(alarmPin, OUTPUT);
digitalWrite(alarmPin, LOW);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
incrementTime();
}
if (currentMode == NORMAL) {
displayClockAndAlarm();
handleButtons();
} else if (currentMode == CLOCK_SET) {
displayClockSetting();
handleClockSetModeButtons();
} else if (currentMode == ALARM_SET) {
displayAlarmSetting();
handleAlarmSetModeButtons();
}
}
void incrementTime() {
clockSecond++;
if (clockSecond == 60) {
clockSecond = 0;
clockMinute++;
if (clockMinute == 60) {
clockMinute = 0;
clockHour++;
if (clockHour == 24) {
clockHour = 0;
}
}
}
}
void displayClockAndAlarm() {
lcd.setCursor(0, 0);
lcd.print("Clock: ");
if (clockHour < 10) lcd.print("0");
lcd.print(clockHour);
lcd.print(":");
if (clockMinute < 10) lcd.print("0");
lcd.print(clockMinute);
lcd.print(":");
if (clockSecond < 10) lcd.print("0");
lcd.print(clockSecond);
lcd.setCursor(0, 1);
lcd.print("Alarm: ");
if (alarmHour < 10) lcd.print("0");
lcd.print(alarmHour);
lcd.print(":");
if (alarmMinute < 10) lcd.print("0");
lcd.print(alarmMinute);
lcd.print(alarmActive ? " ON " : " OFF");
}
void handleButtons() {
unsigned long buttonCurrentMillis = millis();
if (buttonCurrentMillis - buttonPreviousMillis >= buttonInterval) {
buttonPreviousMillis = buttonCurrentMillis;
if (digitalRead(setButtonPin) == LOW) {
currentMode = (currentMode == NORMAL) ? CLOCK_SET : NORMAL;
lcd.clear();
}
}
}
void displayClockSetting() {
lcd.setCursor(0, 0);
lcd.print("Set Clock: ");
if (clockHour < 10) lcd.print("0");
lcd.print(clockHour);
lcd.print(":");
if (clockMinute < 10) lcd.print("0");
lcd.print(clockMinute);
lcd.print(":");
if (clockSecond < 10) lcd.print("0");
lcd.print(clockSecond);
lcd.setCursor(0, 1);
lcd.print("Set Alarm: ");
if (alarmHour < 10) lcd.print("0");
lcd.print(alarmHour);
lcd.print(":");
if (alarmMinute < 10) lcd.print("0");
lcd.print(alarmMinute);
}
void handleClockSetModeButtons() {
unsigned long buttonCurrentMillis = millis();
if (buttonCurrentMillis - buttonPreviousMillis >= buttonInterval) {
buttonPreviousMillis = buttonCurrentMillis;
if (digitalRead(setButtonPin) == LOW) {
currentMode = (currentMode == CLOCK_SET) ? NORMAL : ALARM_SET;
lcd.clear();
} else if (digitalRead(leftButtonPin) == LOW) {
cursorPosition = (cursorPosition == 0) ? 1 : 0;
} else if (digitalRead(rightButtonPin) == LOW) {
cursorPosition = (cursorPosition == 0) ? 1 : 0;
} else if (digitalRead(upButtonPin) == LOW) {
if (cursorPosition == 0) {
clockHour = (clockHour + 1) % 24;
} else if (cursorPosition == 1) {
clockMinute = (clockMinute + 1) % 60;
} else {
clockSecond = (clockSecond + 1) % 60;
}
} else if (digitalRead(downButtonPin) == LOW) {
if (cursorPosition == 0) {
clockHour = (clockHour - 1 + 24) % 24;
} else if (cursorPosition == 1) {
clockMinute = (clockMinute - 1 + 60) % 60;
} else {
clockSecond = (clockSecond - 1 + 60) % 60;
}
}
}
}
void displayAlarmSetting() {
lcd.setCursor(0, 0);
lcd.print("Set Clock: ");
if (clockHour < 10) lcd.print("0");
lcd.print(clockHour);
lcd.print(":");
if (clockMinute < 10) lcd.print("0");
lcd.print(clockMinute);
lcd.print(":");
if (clockSecond < 10) lcd.print("0");
lcd.print(clockSecond);
lcd.setCursor(0, 1);
lcd.print("Set Alarm: ");
if (alarmHour < 10) lcd.print("0");
lcd.print(alarmHour);
lcd.print(":");
if (alarmMinute < 10) lcd.print("0");
lcd.print(alarmMinute);
lcd.print(alarmActive ? " ON " : " OFF");
}
void handleAlarmSetModeButtons() {
unsigned long buttonCurrentMillis = millis();
if (buttonCurrentMillis - buttonPreviousMillis >= buttonInterval) {
buttonPreviousMillis = buttonCurrentMillis;
if (digitalRead(setButtonPin) == LOW) {
currentMode = (currentMode == ALARM_SET) ? NORMAL : CLOCK_SET;
lcd.clear();
} else if (digitalRead(leftButtonPin) == LOW) {
cursorPosition = (cursorPosition == 0) ? 2 : 0;
} else if (digitalRead(rightButtonPin) == LOW) {
cursorPosition = (cursorPosition == 0) ? 2 : 0;
} else if (digitalRead(upButtonPin) == LOW) {
if (cursorPosition == 0) {
alarmHour = (alarmHour + 1) % 24;
} else if (cursorPosition == 2) {
alarmMinute = (alarmMinute + 1) % 60;
} else {
alarmActive = !alarmActive;
}
} else if (digitalRead(downButtonPin) == LOW) {
if (cursorPosition == 0) {
alarmHour = (alarmHour - 1 + 24) % 24;
} else if (cursorPosition == 2) {
alarmMinute = (alarmMinute - 1 + 60) % 60;
} else {
alarmActive = !alarmActive;
}
}
}
}