#define CLK 4
#define DIO 5
uint8_t digits[] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};
void start() {
digitalWrite(CLK, HIGH);
digitalWrite(DIO, HIGH);
delayMicroseconds(5);
digitalWrite(DIO, LOW);
digitalWrite(CLK, LOW);
delayMicroseconds(5);
}
void stop() {
digitalWrite(CLK, LOW);
digitalWrite(DIO, LOW);
delayMicroseconds(5);
digitalWrite(CLK, HIGH);
digitalWrite(DIO, HIGH);
delayMicroseconds(5);
}
bool writeByte(uint8_t value) {
for (uint8_t i = 0; i < 8; i++) {
digitalWrite(CLK, LOW);
digitalWrite(DIO, (value >> i) & 0x01);
delayMicroseconds(5);
digitalWrite(CLK, HIGH);
delayMicroseconds(5);
}
// Wait for ACK
digitalWrite(CLK, LOW);
pinMode(DIO, INPUT);
delayMicroseconds(5);
digitalWrite(CLK, HIGH);
delayMicroseconds(5);
pinMode(DIO, OUTPUT);
return true;
}
void displayDigits(uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4) {
start();
writeByte(0x40); // Set auto-increment mode
stop();
start();
writeByte(0xC0); // Set starting address
writeByte(d1);
writeByte(d2);
writeByte(d3);
writeByte(d4);
stop();
start();
writeByte(0x8F); // Display control: max brightness
stop();
}
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
void setup() {
pinMode(CLK, OUTPUT);
pinMode(DIO, OUTPUT);
Wire.begin();
if (!rtc.begin()) {
while (1); // Halt if RTC is not found
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
static bool showTime = true;
static unsigned long lastSwitch = 0;
unsigned long currentMillis = millis();
if (currentMillis - lastSwitch >= 5000) {
showTime = !showTime;
lastSwitch = currentMillis;
}
DateTime now = rtc.now();
if (showTime) {
displayDigits(digits[now.hour() / 10], digits[now.hour() % 10], digits[now.minute() / 10], digits[now.minute() % 10]);
} else {
displayDigits(digits[now.day() / 10], digits[now.day() % 10], digits[now.month() / 10], digits[now.month() % 10]);
}
delay(1000);
}