#include <LiquidCrystal.h>
#include <RTClib.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
RTC_DS1307 rtc;
#define RED 9
#define GREEN 10
#define BLUE 11
#define BTN_THEME 8
#define BTN_MODE 12
int theme = 0;
bool analogMode = false;
void setColor(int r, int g, int b) {
analogWrite(RED, r);
analogWrite(GREEN, g);
analogWrite(BLUE, b);
}
void autoDayNight(int hour) {
if (hour >= 6 && hour < 18) {
setColor(0, 150, 255); // Day
} else {
setColor(150, 0, 255); // Night
}
}
void manualTheme() {
switch(theme) {
case 1: setColor(255,0,0); break;
case 2: setColor(0,255,0); break;
case 3: setColor(0,0,255); break;
case 4: setColor(255,255,0); break;
default: autoDayNight(rtc.now().hour());
}
}
void drawAnalog(int h, int m) {
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Analog Clock");
int pos = map(m, 0, 59, 0, 15);
lcd.setCursor(pos,1);
lcd.print("*");
}
void setup() {
lcd.begin(16,2);
if (!rtc.begin()) {
lcd.print("RTC ERROR");
while(1);
}
// SET TIME AUTOMATICALLY (FIRST RUN ONLY)
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(BTN_THEME, INPUT_PULLUP);
pinMode(BTN_MODE, INPUT_PULLUP);
}
void loop() {
DateTime now = rtc.now();
// Theme Button
if (!digitalRead(BTN_THEME)) {
theme++;
if (theme > 4) theme = 0;
delay(300);
}
// Mode Button
if (!digitalRead(BTN_MODE)) {
analogMode = !analogMode;
delay(300);
}
manualTheme();
if (analogMode) {
drawAnalog(now.hour(), now.minute());
} else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(now.timestamp(DateTime::TIMESTAMP_TIME));
lcd.setCursor(0,1);
lcd.print(now.timestamp(DateTime::TIMESTAMP_DATE));
}
delay(1000);
}