#include <Wire.h>
#include "RTClib.h"
#include "SevSeg.h"
RTC_DS1307 rtc;
SevSeg sevseg;
// Variables for timing update
unsigned long prevMillis = 0;
const long interval = 1000; // Update every 1 second
void setup() {
Serial.begin(9600);
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// If the RTC is not running, set it to the time this sketch was compiled
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// --- 7-Segment Display Configuration ---
byte numDigits = 4;
// Digit Pins (D1, D2, D3, D4 mapped to pins 10, 11, 12, 13)
byte digitPins[] = {10, 11, 12, 13};
// Segment Pins (A, B, C, D, E, F, G, DP mapped to pins 2 to 9)
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
bool resistorsOnSegments = false; // Set true if using physical resistors on segments
byte hardwareConfig = COMMON_ANODE; // Change to COMMON_CATHODE if display shows inverted/blank logic
bool updateWithDelays = false;
bool leadingZeros = true;
bool disableDecPoint = false;
// Initialize the display
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop() {
unsigned long currentMillis = millis();
// Read the RTC time periodically
if (currentMillis - prevMillis >= interval) {
prevMillis = currentMillis;
DateTime now = rtc.now();
// Format time as HHMM (e.g., 14:30 becomes integer 1430)
int displayTime = (now.hour() * 100) + now.minute();
// Send the integer to the display buffer, lighting up the second decimal point as a colon
sevseg.setNumber(displayTime, 2);
}
// Must be called continuously to refresh the multiplexed display
sevseg.refreshDisplay();
}