#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TimerOne.h>
// Set the LCD I2C address and dimensions
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Set Pins
const int modeButton = 2; // Change mode
const int okButton = 3; // Up value
const int incButton = 4; // Down value
const int buzzerPin = 5; // Buzzer pin
// Variables to hold time and alarm values
int hours = 0;
int minutes = 0;
int seconds = 0;
// Temporary time
int tempHours = 0;
int tempMins = 0;
int tempSecs = 0;
// Stopwatch settings
int stopHund = 0;
int stopHours = 0;
int stopMins = 0;
int stopSecs = 0;
// Start stopwatch flag
int startSW = 0;
// Alarm settings
int alEnable = 0;
int alHours = 0;
int alMins = 0;
// Temporary alarm time
int tempalHours = 0;
int tempalMins = 0;
// Mode settings
int mode = 0;
int dispMode = 0;
// Settings submode
int settingsMode = 0;
int clockMode = 0;
int alarmMode = 0;
void setup(){
lcd.init();
lcd.backlight();
pinMode(modeButton, INPUT);
pinMode(okButton, INPUT);
pinMode(incButton, INPUT);
pinMode(buzzerPin, OUTPUT);
Timer1.initialize(10000);
Timer1.attachInterrupt(incrementTime);
}
void loop(){
if (digitalRead(modeButton) == HIGH){
mode = (mode + 1) % 4;
lcd.clear();
delay(200);
if (mode == 2){
clockMode = 0;
tempHours = 0;
tempMins = 0;
tempSecs = 0;
}
if (mode == 3){
alarmMode = 0;
tempalHours = 0;
tempalMins = 0;
}
}
if (mode == 0){
dispClock();
}
if (mode == 1){
stopwatch();
}
if (mode == 2){
setClock();
}
if (mode == 3){
setAlarm();
}
}
int count = 0;
void incrementTime(){
if (count == 100){
count = 0;
// Set time
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
}
if (startSW){
stopHund++;
if (stopHund >= 100){
stopHund = 0;
stopSecs++;
if (stopSecs >= 60) {
stopSecs = 0;
stopMins++;
if (stopMins >= 60) {
stopMins = 0;
stopHours++;
if (stopHours >= 24) {
stopHours = 0;
}
}
}
}
}
count++;
}
void dispClock(){
if (alEnable == 1 && hours == alHours && minutes == alMins){
dispMode = 1;
lcd.clear();
}
if (dispMode == 0){
// Current time
lcd.setCursor(0, 0);
lcd.print("Time: ");
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);
// Next alarm
lcd.setCursor(0, 1);
lcd.print("Alarm: ");
if (alEnable){
if (alHours < 10) lcd.print("0");
lcd.print(alHours);
lcd.print(":");
if (alMins < 10) lcd.print("0");
lcd.print(alMins);
}
else {
lcd.print("-");
}
}
if (dispMode == 1){
lcd.setCursor(0, 0);
lcd.print("Alarm");
lcd.setCursor(0, 1);
if (alHours < 10) lcd.print("0");
lcd.print(alHours);
lcd.print(":");
if (alMins < 10) lcd.print("0");
lcd.print(alMins);
lcd.noBacklight();
tone(buzzerPin, 500);
delay(300);
lcd.backlight();
noTone(buzzerPin);
delay(200);
if (digitalRead(okButton) == HIGH) {
dispMode = (dispMode + 1) % 2;
lcd.clear();
delay(200);
}
}
}
void stopwatch() {
lcd.setCursor(0, 0);
lcd.print("Stopwatch");
lcd.setCursor(0, 1);
if (stopHours < 10) lcd.print("0");
lcd.print(stopHours);
lcd.print(":");
if (stopMins < 10) lcd.print("0");
lcd.print(stopMins);
lcd.print(":");
if (stopSecs < 10) lcd.print("0");
lcd.print(stopSecs);
lcd.print(".");
if (stopHund < 10) lcd.print("0");
lcd.print(stopHund);
if (digitalRead(okButton) == HIGH) {
startSW = !startSW;
delay(200);
}
if (digitalRead(incButton) == HIGH) {
stopHours = 0;
stopMins = 0;
stopSecs = 0;
stopHund = 0;
}
}
void setClock(){
if (digitalRead(okButton) == HIGH) {
if (clockMode == 1){
hours = tempHours;
}
if (clockMode == 2){
minutes = tempMins;
}
if (clockMode == 3){
seconds = tempSecs;
}
clockMode = (clockMode + 1) % 4;
lcd.clear();
delay(200);
}
if (clockMode == 0){
lcd.setCursor(0, 0);
lcd.print("Clock Settings");
lcd.setCursor(0, 1);
lcd.print("Press OK to Set");
}
if (clockMode == 1){
lcd.setCursor(0, 0);
lcd.print("Enter hours:");
lcd.setCursor(0, 1);
lcd.print("> ");
if (tempHours < 10) lcd.print("0");
lcd.print(tempHours);
if (digitalRead(incButton) == HIGH){
tempHours = (tempHours + 1) % 24;
delay(200);
}
}
if (clockMode == 2){
lcd.setCursor(0, 0);
lcd.print("Enter minutes:");
lcd.setCursor(0, 1);
lcd.print("> ");
if (tempMins < 10) lcd.print("0");
lcd.print(tempMins);
if (digitalRead(incButton) == HIGH){
tempMins = (tempMins + 1) % 60;
delay(200);
}
}
if (clockMode == 3){
lcd.setCursor(0, 0);
lcd.print("Enter seconds:");
lcd.setCursor(0, 1);
lcd.print("> ");
if (tempSecs < 10) lcd.print("0");
lcd.print(tempSecs);
if (digitalRead(incButton) == HIGH){
tempSecs = (tempSecs + 1) % 60;
delay(200);
}
}
}
void setAlarm(){
if (digitalRead(okButton) == HIGH) {
if (alarmMode == 2){
alHours = tempalHours;
}
if (alarmMode == 3){
alMins = tempalMins;
}
alarmMode = (alarmMode + 1) % 4;
lcd.clear();
delay(200);
}
if (alarmMode == 0){
lcd.setCursor(0, 0);
lcd.print("Alarm Settings");
lcd.setCursor(0, 1);
lcd.print("Press OK to Set");
}
if (alarmMode == 1){
lcd.setCursor(0, 0);
lcd.print("Enable alarm?");
lcd.setCursor(0, 1);
lcd.print("> ");
if (alEnable){
lcd.print("Enabled");
}
else {
lcd.print("Disabled");
}
if (digitalRead(incButton) == HIGH){
alEnable = (alEnable + 1) % 2;
lcd.clear();
delay(200);
}
}
if (alarmMode == 2){
lcd.setCursor(0, 0);
lcd.print("Alarm hours:");
lcd.setCursor(0, 1);
lcd.print("> ");
if (tempalHours < 10) lcd.print("0");
lcd.print(tempalHours);
if (digitalRead(incButton) == HIGH){
tempalHours = (tempalHours + 1) % 24;
delay(200);
}
}
if (alarmMode == 3){
lcd.setCursor(0, 0);
lcd.print("Alarm minutes:");
lcd.setCursor(0, 1);
lcd.print("> ");
if (tempalMins < 10) lcd.print("0");
lcd.print(tempalMins);
if (digitalRead(incButton) == HIGH){
tempalMins = (tempalMins + 1) % 60;
delay(200);
}
}
}