Help with completing sketch for LED Clock

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.

Hello,

I think you may be interested by this library and its examples

Thanks for the tip, but I don't have any code writing ability. I can follow instructions on how to add code to the sketch, but not to create it, or where to insert it. Or if any of the existing code needs to be modified or removed.

No-one is born with this ability. You must try to learn, if you want to have Arduino as your hobby. The forum will help you as much as you need, provided you are trying.

Modifying code which already exists and is complex is not a good way to begin to learn coding, that curve is too steep. Begin with writing your own very short very simple code, and build up from there to increase your skills gradually.

The section of code that deals with the time-set buttons appears to be this:

if (digitalRead(hrs_set) == HIGH)  // is the "set hours" button pressed?
  {
    hrs_var += 1;                       // increment the hour number
    if (hrs_var > 23) hrs_var = 0;      // wrap it around if needed
    rtc.setTime(hrs_var, min_var, 0);   // store it in the RTC module
    for (int i = 0; i < 1000; i ++)     // no idea about this loop, sorry.
    {
      Display.setNumber(number);
      Display.refreshDisplay();
    }
  }
  if (digitalRead(min_set) == HIGH)  // now, do the same for the minutes
  {
    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();
    }
  }

At the end of the loop, the code checks to see if either button is pressed (the digitalRead() lines) and then increments the hours or minutes for each button press. You would need to add the definitions for the buttons and check to see that any other variables are named correctly.

I am trying to do something similar at the moment, but have gone a different way. I have three buttons, "Set", "plus" and "minus", where the "set" button takes me into setup mode, then moves from hour to minute to day to month to year, then to give me an option to save the time or not. Adds a little more complexity to the code, but not all that much.

This is a single project that required an Arduino to make it work. So Arduino is not a hobby for me, but a necessity. I have tried in the past to learn to write code, even bought several books on the subject, but have been unable to move past the basic understanding of how to see where it all fits together. Its like trying to learn a new language for me, which has also something I'm no good at. I am good at the hardware part, not the software.

The clock I want to eventually make will be a replica of a Nixie VFD tube clock using SMD LED displays and glass test tubes to simulate vacuum tubes. Extensive searching on the Internet did not turn up exactly what I was searching for without some modification to the sketch provided.

I hope that a solution can be found, as I have a breadboarded prototype clock that displays time in six digits, and has a PM indicator, but lacks a means to set the time. The original sketch had this feature, bud did not have 6 digit display. The modified sketch had six digits, but no time setting buttons. And there were several requests at that site for the same help, including myself, but no reply.

So would it be easier to modify the original sketch to include seconds in the display, instead of trying to restore the time setting buttons to the modified version?

I was trying to keep it simple, just two buttons (one to set hours, the other to set minutes) like in the original sketch. It works as is, but doesn't have seconds in the LED display. Instead, it uses a blinking LED to show seconds. This will eventually be a gift for a person that needs a simple way to set the time.

Either should be pretty easy, both sketches are short and simple. Take your pick and have a go.

I already have tried, and couldn't figure it out. That is why I was asking for help.

I think the issue here is that the forum is really intended to provide help to people who are having problems, what you seem to want is for someone to write the code for you, which is quite different.

As for learning to program, all I can suggest is that you approach it in very basic steps, not moving to the next step until you fully understand that one. Unfortunately, if you're not in this as a hobby to learn about Arduino and what it can do and then move on to other projects, that will seem like a waste of time.

Merging the two pieces of code isn't as simple as it looks (I did just have a go) because the two use different libraries to handle various elements. So it would be simpler to use the setting part as a guide rather than just trying to copy/paste code.

The goal isn't to merge the two sketches, but rather to complete either one with the missing function. For example, the original sketch having six instead of four digits for the time display, or the second sketch having the time-setting buttons added. Whichever would be simpler.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.