#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
// Define the pins for the 74HC595 shift register
const int dataPin = 4; // DS (data pin)
const int clockPin = 5; // SHCP (clock pin)
const int latchPin = 6; // STCP (latch pin)
// Define the segment pins
const int segmentPins[] = {7, 8, 9, 10, 11, 12, 13};
void setup() {
Wire.begin();
rtc.begin();
// Set the shift register pins as outputs
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
// Set the segment pins as outputs
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Uncomment the following line if you want to set the time on the RTC
// rtc.adjust(DateTime(__DATE__, __TIME__));
}
void loop() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
// Display time on the 8-digit seven-segment display
displayTime(hour, minute, second);
delay(1000); // Update every second
}
void displayTime(int hour, int minute, int second) {
byte digits[] = {
B11111100, // Digit 0 (segment a, b, c, d, e, f)
B01100000, // Digit 1 (segment b, c)
B11011010, // Digit 2 (segment a, b, d, e, g)
B11110010, // Digit 3 (segment a, b, c, d, g)
B01100110, // Digit 4 (segment b, c, f, g)
B10110110, // Digit 5 (segment a, c, d, f, g)
B10111110, // Digit 6 (segment a, c, d, e, f, g)
B11100000, // Digit 7 (segment a, b, c)
};
// Loop through the digits
for (int digit = 0; digit < 8; digit++) {
// Shift out the digit data
shiftOut(dataPin, clockPin, MSBFIRST, digits[digit]);
// Enable the current digit
digitalWrite(latchPin, LOW);
digitalWrite(segmentPins[digit], HIGH);
digitalWrite(latchPin, HIGH);
// Delay to display the digit
delay(2);
// Disable the current digit
digitalWrite(latchPin, LOW);
digitalWrite(segmentPins[digit], LOW);
digitalWrite(latchPin, HIGH);
}
}