/*
Project: 7 Segment Clock
Description: In a real circuit add resistors on each segment
and transistors on each digit! (Wokwi doesn't care...)
The gray wires are digits, colored are the segments.
Written for common cathode displays.
Creation date: 5/11/24
Author: AnonEngineering
License: https://en.wikipedia.org/wiki/Beerware
*/
#include "RTClib.h"
const int INTERVAL = 1000;
const int MAX_DIGITS = 6;
// pin definitions
const int DP_PIN = 7;
const int DIGIT_PINS[MAX_DIGITS] = {A1, A2, A3, 2, 3, 4};
const int SEG_PINS[8] = {10, 12, 6, 8, 9, 11, 5, DP_PIN}; // segments A-G, DP
// lookup table, which segments are on for each value
const bool NUM_SEGS[11][8] = { // segment lookup table
{1, 1, 1, 1, 1, 1, 0, 0}, /* 0 */
{0, 1, 1, 0, 0, 0, 0, 0}, /* 1 */
{1, 1, 0, 1, 1, 0, 1, 0}, /* 2 */
{1, 1, 1, 1, 0, 0, 1, 0}, /* 3 */
{0, 1, 1, 0, 0, 1, 1, 0}, /* 4 */
{1, 0, 1, 1, 0, 1, 1, 0}, /* 5 */
{1, 0, 1, 1, 1, 1, 1, 0}, /* 6 */
{1, 1, 1, 0, 0, 0, 0, 0}, /* 7 */
{1, 1, 1, 1, 1, 1, 1, 0}, /* 8 */
{1, 1, 1, 1, 0, 1, 1, 0}, /* 9 */
{0, 0, 0, 0, 0, 0, 0, 0} /* Blank */
};
unsigned long prevTime = INTERVAL;
RTC_DS1307 rtc;
// for hardware test
void testDisplay() {
writeDigit(0, 1);
writeDigit(1, 2);
writeDigit(2, 3);
writeDigit(3, 4);
writeDigit(4, 5);
writeDigit(5, 6);
}
void updateDisplay(int hour, int minute, int second) {
// lead zero blanking (10 is a blank char)
int disp10Hour = hour < 10 ? 10 : hour / 10;
int disp01Hour = hour % 10;
int disp10Min = minute / 10;
int disp01Min = minute % 10;
int disp10Sec = second / 10;
int disp01Sec = second % 10;
// write values to display
writeDigit(0, disp10Hour);
writeDigit(1, disp01Hour);
writeDigit(2, disp10Min);
writeDigit(3, disp01Min);
writeDigit(4, disp10Sec);
writeDigit(5, disp01Sec);
}
void writeDigit(int digit, int number) {
for (int segment = 0; segment < 8; segment++) {
digitalWrite(SEG_PINS[segment], NUM_SEGS[number][segment]);
}
if (digit == 1 || digit == 3) digitalWrite(DP_PIN, HIGH);
// turn digit on & off
digitalWrite(DIGIT_PINS[digit], LOW);
digitalWrite(DIGIT_PINS[digit], HIGH);
}
void setup() {
Serial.begin(115200); // for test
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while ( true );
}
for (int i = 0; i < MAX_DIGITS; i++) {
pinMode(DIGIT_PINS[i], OUTPUT);
digitalWrite(DIGIT_PINS[i], HIGH);
}
}
void loop() {
char buffer[32];
int h, m, s;
if (millis() - prevTime >= INTERVAL) {
prevTime = millis();
DateTime now = rtc.now();
h = now.hour();
m = now.minute();
s = now.second();
snprintf(buffer, 32, "Time: %d/%d/%d %d:%02d:%02d",
now.month(), now.day(), now.year(), h, m, s);
Serial.println(buffer);
}
//testDisplay();
updateDisplay(h, m, s);
}