I have a project that requires assistance with completing a sketch for a 6 digit LED clock (seven segment single digits). The problem I am having is in adding the two buttons to set the time (one for hour and one for minute adjustment).
The original sketch was for a 4 digit clock that had the buttons included, but the modified sketch eliminated the buttons and added the two digits for seconds. The two sketches came from different sources, and I can't contact either person that wrote them. The original sketch is listed below;
#include "SevSeg.h"
#include <DS3231.h>
DS3231 rtc(SDA, SCL);
Time t;
SevSeg Display;
const int hrs_set = A0;
const int min_set = A1;
const int ledPin = A3;
unsigned int number = 0;
const long interval = 500;
unsigned long startMillis;
unsigned long currentMillis;
unsigned long previousMillis = 0;
unsigned int Hour = 0;
unsigned int hrs_var = 0;
unsigned int min_var = 0;
int ledState = HIGH;
void setup()
{
rtc.begin();
pinMode(ledPin, OUTPUT);
pinMode(hrs_set, INPUT_PULLUP);
pinMode(min_set, INPUT_PULLUP);
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13, A2};
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
bool resistorsOnSegments = false;
bool updateWithDelays = false;
byte hardwareConfig = COMMON_ANODE;
bool leadingZeros = false;
bool disableDecPoint = true;
Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, disableDecPoint);
Display.setBrightness(60);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == HIGH)
{
ledState = LOW;
}
else
{
ledState = HIGH;
}
digitalWrite(ledPin, ledState);
}
t = rtc.getTime();
Hour = t.hour;
hrs_var = t.hour;
min_var = t.min;
if (t.hour > 12)
{
if (t.hour == 13) Hour = 1;
if (t.hour == 14) Hour = 2;
if (t.hour == 15) Hour = 3;
if (t.hour == 16) Hour = 4;
if (t.hour == 17) Hour = 5;
if (t.hour == 18) Hour = 6;
if (t.hour == 19) Hour = 7;
if (t.hour == 20) Hour = 8;
if (t.hour == 21) Hour = 9;
if (t.hour == 22) Hour = 10;
if (t.hour == 23) Hour = 11;
}
else
{
if (t.hour == 0) Hour = 12;
}
number = Hour * 100 + t.min;
Display.setNumber(number);
Display.refreshDisplay();
if (digitalRead(hrs_set) == HIGH)
{
hrs_var += 1;
if (hrs_var > 23) hrs_var = 0;
rtc.setTime(hrs_var, min_var, 0);
for (int i = 0; i < 1000; i ++)
{
Display.setNumber(number);
Display.refreshDisplay();
}
}
if (digitalRead(min_set) == HIGH)
{
min_var += 1;
if (min_var >= 60) min_var = 0;
rtc.setTime(hrs_var, min_var, 0);
for (int i = 0; i < 1000; i ++)
{
Display.setNumber(number);
Display.refreshDisplay();
}
}
}
And the modified sketch is here;
#include "SevSeg.h"
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
SevSeg Display;
// const int hrs_set = A0;
// const int min_set = A1;
const int ledPin = A3;
unsigned long timeDisplay;
unsigned long currentMillis;
unsigned int Hour;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 500;
void setup()
{
pinMode(ledPin, OUTPUT);
byte numDigits = 6;
byte digitPins[] = {9, 10, 11, 12, 13, A2};
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
bool resistorsOnSegments = false; // false = resistors are on digit pins
bool updateWithDelaysIn = true;
byte hardwareConfig = COMMON_ANODE;
Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
Display.setBrightness(60);
}
void loop()
{
tmElements_t tm;
if (RTC.read(tm))
{
Hour = tm.Hour;
if (tm.Hour >= 12)
{
if (tm.Hour == 13) Hour = 1;
if (tm.Hour == 14) Hour = 2;
if (tm.Hour == 15) Hour = 3;
if (tm.Hour == 16) Hour = 4;
if (tm.Hour == 17) Hour = 5;
if (tm.Hour == 18) Hour = 6;
if (tm.Hour == 19) Hour = 7;
if (tm.Hour == 20) Hour = 8;
if (tm.Hour == 21) Hour = 9;
if (tm.Hour == 22) Hour = 10;
if (tm.Hour == 23) Hour = 11;
ledState = HIGH; // PM
}
else
{
if (tm.Hour == 0) Hour = 12;
ledState = LOW; // AM
}
digitalWrite(ledPin, ledState);
timeDisplay = (Hour * 100 + tm.Minute) * 100L + tm.Second;
}
else
{
timeDisplay = 888888; // error
}
Display.setNumber(timeDisplay);
Display.refreshDisplay();
}
The sketch is running on a Uno v3 board with a DS3231 RTC module compiled on IDE version 1.8.13 on a Windows 10 PC. As of now everything is working; even though the modified code mentions a DS1307, the circuit is using a DS3231, which I found out will work as long as the DS3231 has been set for the correct time prior to connecting it to the circuit. I have modified the original wiring diagram to reflect what I now have, and hope to be able to use when the sketch is complete.
