//SimpleClock
//by Myth!
// This code controls a TM1637-based LED display to show the current time
// obtained from a DS1307 real-time clock (RTC).
#include <RTClib.h>
#include <TM1637.h>
#define CLK_PIN 2 // Pin connected to the CLK (clock) input of the LED display
#define DIO_PIN 3 // Pin connected to the DIO (data) input of the LED display
RTC_DS1307 rtc; // Object for interacting with the DS1307 real-time clock
TM1637 display(CLK_PIN, DIO_PIN); // Object for controlling the TM1637 LED display
void setup() {
display.set(BRIGHT_TYPICAL); // Set the LED display brightness to a typical level
if (!rtc.begin()) { // Initialize the real-time clock
while (1); // Hang here indefinitely if RTC initialization fails
}
if (!rtc.isrunning()) { // If the RTC is not running (e.g., due to power loss)
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set the RTC to the compile time
}
}
void loop() {
DateTime now = rtc.now(); // Get the current date and time from the RTC
if (now.second() % 2 == 0) { // Toggle the colon on the display every second
display.point(POINT_ON); // Turn on the colon
} else {
display.point(POINT_OFF); // Turn off the colon
}
// Display the hour (with leading zero if necessary) on the first two digits
display.display(0, now.hour() < 10 ? 0 : now.hour() / 10);
display.display(1, now.hour() % 10);
// Display the minute (with leading zero if necessary) on the last two digits
display.display(2, now.minute() < 10 ? 0 : now.minute() / 10);
display.display(3, now.minute() % 10);
}