#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
// Define the pins for the TM1637
const int CLK = D8; // Connect to CLK pin
const int DIO = D9; // Connect to DIO pin
// Segment mapping for numbers 0-9
const int segmentMapping[10][8] = {
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW}, // 0
{LOW, LOW, HIGH, LOW, LOW, LOW, LOW}, // 1
{HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH}, // 2
{HIGH, HIGH, LOW, HIGH, LOW, LOW, HIGH}, // 3
{LOW, HIGH, HIGH, LOW, LOW, HIGH, LOW}, // 4
{HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH}, // 5
{HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH}, // 6
{HIGH, HIGH, LOW, LOW, LOW, LOW, LOW}, // 7
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}, // 8
{HIGH, HIGH, HIGH, LOW, LOW, HIGH, HIGH} // 9
};
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
// Check if RTC is connected
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Set the time if it’s not already set
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
pinMode(CLK, OUTPUT);
pinMode(DIO, OUTPUT);
}
void loop() {
DateTime now = rtc.now();
int displayTime = now.hour() * 100 + now.minute(); // Format HHMM
displayNumber(displayTime); // Display the formatted time
delay(1000);
}
void displayNumber(int number) {
int digits[4];
digits[0] = number / 1000; // Thousands
digits[1] = (number / 100) % 10; // Hundreds
digits[2] = (number / 10) % 10; // Tens
digits[3] = number % 10; // Units
for (int i = 0; i < 4; i++) {
showDigit(i, digits[i]);
delay(5); // Short delay for multiplexing
}
}
void showDigit(int position, int number) {
// You would need to turn on the correct segments here
// Activate the digit (this is pseudo-code for understanding)
// In reality, you would send data through CLK and DIO to control the display
// You can implement the segment activation logic here.
// Use the segmentMapping array to set the pins accordingly.
}