#include <LiquidCrystal.h>
#include <Keypad.h>
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
int hours = 0;
int minutes = 0;
int seconds = 0;
bool settingTime = false;
int settingStep = 0;
String currentInput = "";
void showSplashScreen() {
lcd.print("ZhaoYiNan");
lcd.setCursor(0, 1);
lcd.print("20232241082");
delay(2000);
lcd.clear();
}
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
showSplashScreen();
lcd.clear();
lcd.noCursor();
}
void displayTime() {
lcd.setCursor(0, 1);
if (hours < 10) lcd.print('0');
lcd.print(hours);
lcd.print(':');
if (minutes < 10) lcd.print('0');
lcd.print(minutes);
lcd.print(':');
if (seconds < 10) lcd.print('0');
lcd.print(seconds);
lcd.setCursor(0, 0);
lcd.print("Current time:");
lcd.noCursor();
}
void displaySetTime() {
lcd.setCursor(0, 1);
if (hours < 10) lcd.print('0');
lcd.print(hours);
lcd.print(':');
if (minutes < 10) lcd.print('0');
lcd.print(minutes);
lcd.print(':');
if (seconds < 10) lcd.print('0');
lcd.print(seconds);
}
void loop() {
if (!settingTime) {
char key = keypad.getKey();
if (key) {
if (key == 'A') { // Enter setting mode
settingTime = true;
settingStep = 0;
currentInput = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Hours: ");
lcd.setCursor(11, 0);
lcd.cursor();
}
} else {
delay(1000);
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
}
if (minutes >= 60) {
minutes = 0;
hours++;
}
if (hours >= 24) {
hours = 0;
}
displayTime();
}
} else {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
if (currentInput.length() < 2) {
currentInput += key;
lcd.print(key);
}
} else if (key == '*') { // Delete last character
if (currentInput.length() > 0) {
currentInput.remove(currentInput.length() - 1);
lcd.setCursor(11 + currentInput.length(), 0);
lcd.print(' ');
lcd.setCursor(11 + currentInput.length(), 0);
}
} else if (key == '#') { // Confirm current input
int value = currentInput.toInt();
if (settingStep == 0) {
hours = value;
settingStep++;
currentInput = "";
lcd.setCursor(0, 0);
lcd.print("Set Min: ");
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.setCursor(11, 0);
} else if (settingStep == 1) {
minutes = value;
settingStep++;
currentInput = "";
lcd.setCursor(0, 0);
lcd.print("Set Sec: ");
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.setCursor(11, 0);
} else if (settingStep == 2) {
seconds = value;
settingStep++;
settingTime = false;
lcd.noCursor();
lcd.clear();
displayTime();
}
}
}
}
}