#include "Clock.h"
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
const int PIN_BTN_HOUR_DEC = 2;
const int PIN_BTN_HOUR_INC = 1;
const int PIN_BTN_MIN_DEC = 4;
const int PIN_BTN_MIN_INC = 3;
const int PIN_BTN_SEC_ZERO = 5;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
Clock clock;
void displayDateTime() {
DateTime now = clock.now();
lcd.setCursor(0, 0);
lcd.print("Date: " + String(now.day()) + "." + String(now.month()) + "." + String(now.year()));
lcd.setCursor(0, 1);
String nowMin = (now.minute() / 10 == 0) ? "0" + String(now.minute()) : String(now.minute());
String nowSec = (now.second() / 10 == 0) ? "0" + String(now.second()) : String(now.second());
if (now.second() % 2 == 0) {
lcd.print("Time: " + String(now.hour()) + ":" + nowMin + ":" + nowSec);
} else {
lcd.print("Time: " + String(now.hour()) + ":" + nowMin + " " + nowSec);
}
}
void setup() {
Serial.begin(9600);
pinMode(PIN_BTN_HOUR_INC, INPUT_PULLUP);
pinMode(PIN_BTN_HOUR_DEC, INPUT_PULLUP);
pinMode(PIN_BTN_MIN_INC, INPUT_PULLUP);
pinMode(PIN_BTN_MIN_DEC, INPUT_PULLUP);
pinMode(PIN_BTN_SEC_ZERO, INPUT_PULLUP);
lcd.init();
lcd.backlight();
clock.begin();
}
void loop() {
displayDateTime();
if (digitalRead(PIN_BTN_HOUR_INC) != 1) {
clock.incrementHour();
delay(200);
lcd.clear();
}
if (digitalRead(PIN_BTN_HOUR_DEC) != 1) {
clock.decrementHour();
delay(200);
lcd.clear();
}
if (digitalRead(PIN_BTN_MIN_INC) != 1) {
clock.incrementMinute();
Serial.println(clock.now().minute());
delay(200);
lcd.clear();
}
if (digitalRead(PIN_BTN_MIN_DEC) != 1) {
clock.decrementMinute();
Serial.println(clock.now().minute());
delay(200);
lcd.clear();
}
if (digitalRead(PIN_BTN_SEC_ZERO) != 1) {
clock.zeroSecond();
delay(200);
lcd.clear();
}
}