#include <Wire.h>
#include <RTClib.h>
#include <TM1637Display.h>
#define CLK_PIN 9
#define DIO_PIN 8
TM1637Display display(CLK_PIN, DIO_PIN);
RTC_DS1307 rtc;
bool showTime = true;
#define UP_BUTTON_PIN 2
#define DOWN_BUTTON_PIN 3
#define MODE_BUTTON_PIN 4
unsigned long previousMillis = 0;
unsigned long previousMillisDate = 0; // Added for displaying date
const long interval = 10000;
int mode = 0;
void setup() {
Wire.begin();
rtc.begin();
display.setBrightness(7);
pinMode(UP_BUTTON_PIN, INPUT_PULLUP);
pinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);
pinMode(MODE_BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
DateTime now = rtc.now();
if (mode == 0 || mode == 3) {
bool isPM = now.hour() >= 12;
int hour12 = now.hour() % 12;
if (hour12 == 0) {
hour12 = 12;
}
// Display hour and minute with blinking colon for seconds
if (showTime) {
display.showNumberDecEx(hour12 * 100 + now.minute(), 0b01110000, true); // Blinking colon enabled
} else {
display.showNumberDecEx(hour12 * 100 + now.minute(), 0b01110000, false); // Blinking colon disabled
}
} else if (mode == 1) {
display.showNumberDecEx(now.month() * 100 + now.day(), 0b00001110, showTime);
} else if (mode == 2) {
display.showNumberDecEx(0, 0b01111001, false, 3, 1);
display.showNumberDecEx(0, 0b01011110, false, 1, 0);
}
int buttonUpState = digitalRead(UP_BUTTON_PIN);
int buttonDownState = digitalRead(DOWN_BUTTON_PIN);
int buttonModeState = digitalRead(MODE_BUTTON_PIN);
if (buttonUpState == LOW) {
delay(50);
if (buttonUpState == LOW) {
if (mode == 3) {
DateTime newTime = DateTime(now.year(), now.month(), now.day(), now.hour() + 1, now.minute(), now.second());
rtc.adjust(newTime);
} else if (mode == 1) {
DateTime newDate = DateTime(now.year(), now.month(), now.day() + 1, now.hour(), now.minute(), now.second());
rtc.adjust(newDate);
} else {
DateTime newTime = DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute() + 1, now.second());
rtc.adjust(newTime);
}
}
}
if (buttonDownState == LOW) {
delay(50);
if (buttonDownState == LOW) {
if (mode == 3) {
DateTime newTime = DateTime(now.year(), now.month(), now.day(), now.hour() - 1, now.minute(), now.second());
rtc.adjust(newTime);
} else if (mode == 1) {
DateTime newDate = DateTime(now.year(), now.month(), now.day() - 1, now.hour(), now.minute(), now.second());
rtc.adjust(newDate);
} else {
DateTime newTime = DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute() - 1, now.second());
rtc.adjust(newTime);
}
}
}
if (buttonModeState == LOW) {
delay(50);
if (buttonModeState == LOW) {
mode++;
if (mode > 3) {
mode = 0;
}
}
}
if (mode == 2 && buttonModeState == LOW) {
mode = 3;
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
showTime = !showTime;
}
// Display date every 30 seconds
if (currentMillis - previousMillisDate >= 30000) {
display.showNumberDecEx(now.month() * 100 + now.day(), 0b00001110, showTime);
previousMillisDate = currentMillis; // Update the previousMillis for date display
delay(10000); // Display for 10 seconds
}
}