#include <TM1637Display.h>
#include <DS1307RTC.h>
#include <Wire.h>
// Pins for TM1637 display module
#define CLK 2
#define DIO 3
TM1637Display display(CLK, DIO);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize TM1637 display
display.setBrightness(0x0a); // Set brightness level (0x00 to 0x0f)
// Initialize variable to store time
tmElements_t tm;
// Check if RTC is running and read the current time
if (RTC.read(tm)) {
// Convert 24-hour format to 12-hour format
int hour12 = tm.Hour % 12;
if (hour12 == 0) {
hour12 = 12; // 0 hours in 12-hour format is 12 AM
}
// Format time as a string with AM/PM indication
char timeStr[6];
sprintf(timeStr, "%02d%02d", hour12, tm.Minute);
if (tm.Hour >= 12) {
strcat(timeStr, "P");
} else {
strcat(timeStr, "A");
}
// Flashing colon toggle
bool colonVisible = true;
unsigned long lastColonChange = millis();
while (true) {
// Display time on TM1637 display
display.showNumberDecEx(atoi(timeStr), 0b11100000 | (colonVisible ? 0x80 : 0), true);
// Toggle colon every 500 milliseconds
if (millis() - lastColonChange >= 500) {
colonVisible = !colonVisible;
lastColonChange = millis();
}
// Print time to serial monitor
Serial.print("Time: ");
Serial.print(hour12);
Serial.print(":");
Serial.print(tm.Minute);
if (tm.Hour >= 12) {
Serial.println(" PM");
} else {
Serial.println(" AM");
}
delay(1000); // Update every second
}
} else {
Serial.println("RTC read failed!");
// Uncomment the following line to set the time manually
// RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop() {
tmElements_t tm;
// Check if RTC is running and read the current time
if (RTC.read(tm)) {
// Convert 24-hour format to 12-hour format
int hour12 = tm.Hour % 12;
if (hour12 == 0) {
hour12 = 12; // 0 hours in 12-hour format is 12 AM
}
// Format time as a string with AM/PM indication
char timeStr[6];
sprintf(timeStr, "%02d%02d", hour12, tm.Minute);
if (tm.Hour >= 12) {
strcat(timeStr, "P");
} else {
strcat(timeStr, "A");
}
// Flashing colon toggle
bool colonVisible = true;
unsigned long lastColonChange = millis();
while (true) {
// Display time on TM1637 display
display.showNumberDecEx(atoi(timeStr), 0b11100000 | (colonVisible ? 0x80 : 0), true);
// Toggle colon every 500 milliseconds
if (millis() - lastColonChange >= 500) {
colonVisible = !colonVisible;
lastColonChange = millis();
}
// Print time to serial monitor
Serial.print("Time: ");
Serial.print(hour12);
Serial.print(":");
Serial.print(tm.Minute);
if (tm.Hour >= 12) {
Serial.println(" PM");
} else {
Serial.println(" AM");
}
delay(1000); // Update every second
}
} else {
Serial.println("RTC read failed!");
// Uncomment the following line to set the time manually
// RTC.adjust(DateTime(__DATE__, __TIME__));
}
}