Need help adjusting time on Seven Segment Display clock

Remove those lines from your sketch because you have already set the time in the RTC.

Use rtc.adjust() once and never again. Run the "minimal rtc sketch" I posted in Post #5 to set the time in RTC. Never run rtc.adjust() again unless your battery dies, or the RTC is not giving the correct time.

You will need two 595 for a four-digit-seven-segment display. I do not see any "digit" pins that will tell the 4dig-7seg which digit to write the data sent by the 595.

Also, answer the questions asked of you in post #5 through #20.

Which happens twice a year in countries with DST (daylightsavings).
I design my clocks with a WiFi-enabled (ESP32) board, that gets the time off the internet.
My clocks have no buttons and are always showing the right time (with automatic DST).
A RTC should only be used when the clock must be re-started outside your WiFi network.

This short sketch is all you need to get and print the time off the internet.
Enter your WiFi credentials and change the location string to your country (link provided).

#include <WiFi.h> // ESP32
unsigned long prevTime;
time_t now; // holds epoch
tm tm; // create time structure

void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "PW"); // WiFi credentials
  configTzTime("NZST-12NZDT,M9.5.0,M4.1.0/3", "nz.pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}

void loop() {
  time(&now);
  if (now != prevTime) { // time has changed?
    prevTime = now; // remember
    localtime_r(&now, &tm); // compute local time
    printf("\n%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec); // print some of the available time elements
  }
}

There are more time elements available (day of the week, month, year, summer/winter. etc.)

So once I compiled and uploaded that sketch, the display started to display time properly. However, none of the buttons do anything anymore. I clicked on them and nothing changed. My goal for this setup is to have the seven segment displays display the current time in minutes and hours, then if I press a the minute button, the minute incremented forward by one. If the number on minute is 60 at the moment the button was pressed, set it to one. Same for hour button but for the hour number.

Post a wiring diagram... pencil/paper will do.

This is one of my button-less clocks, with three 8x8 LED matrix displays.
USB powered.
Gets the time in a few seconds, Keeps perfect time for days if WiFi goes down.


Just for me, would you move the colon up one row? (:

Could do, but this is what the MD_Parola library came with.

          hour + 1;
          minute + 1;

Neither of these lines of code do anything useful. You are telling the compiler to add 1 to the variable, and then do nothing with the result. The IDE will warn you about this if you have the preferences set to show all warnings:


/tmp/arduino_modified_sketch_796581/test.ino: In function 'void loop()':
/tmp/arduino_modified_sketch_796581/test.ino:66:16: warning: statement has no effect [-Wunused-value]
           hour + 1;
           ~~~~~^~~
/tmp/arduino_modified_sketch_796581/test.ino:84:18: warning: statement has no effect [-Wunused-value]
           minute + 1;
           ~~~~~~~^~~

If you want to increment hour or minute, then do either of the following (but not both);

          hour = hour + 1; //add 1 to hour, then save the result in hour
          hour++; //increments hour by 1

MD_Parola uses the MD_MAX72XX library, which contains the font used for the display.

Here is the schematic of the clock
Raime.pdf (195.5 KB)

Ok I changed it per your recommendation. Nothing happened so far still, the buttons still don't do anything. I put the rtc.adjust(DateTime(F(_DATE_), F(_TIME_))); back in voidsetup because the time get all weird when I removed it. Sometime it will start counting from 00.00, sometimes it will display time but not from my time zone.

//this version is working, don't change it. Make a copy instead.
#include <Adafruit_I2CRegister.h>
#include <RTClib.h>
RTC_DS3231 rtc;
//set up for the Shift registers and numbers
//check the datasheet for the SN74HC595 to see the idividual pins
#define Hourbutton 8//button used to adjust the hour
#define Minutebutton 9//button used to adjust the minutes
#define DS_pin 4//blue
#define STCP_pin 5//yellow
#define SHCP_pin 6//green
//color coded so you don't blows it up
int numbers[10] {126, 48, 109, 121, 51, 91, 95, 112, 127, 123}; //use [int digits [10] { 1, 79, 18, 12, 76, 36, 32, 15, 0, 4};] instead if
//your display is the common Annode type. I did all the binary conversion, you are welcome.
uint8_t digits[4]; //int d1; int d2; int d3; int d4;// has to define all of these, they reperesent the individual digits and the time number
int hbstate;
int mbstate;
int lasthbstate = 0;
int lastmbstate = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(Hourbutton, INPUT);
pinMode(Minutebutton, INPUT);
pinMode(DS_pin, OUTPUT);
pinMode(STCP_pin, OUTPUT);
pinMode(SHCP_pin, OUTPUT);
// initializing the rtc
rtc.begin();
//if (! rtc.begin()) {
    //Serial.println("Couldn't find RTC");
    //Serial.flush();
    //while (1) delay(10);}
  //if (rtc.lostPower()){
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //rtc.adjust(DateTime(2026,6,6,22,40,30);// This line sets the RTC with the time from the pc when compile
  }
void loop() {
  //DateTime hour = now.hour();
  //DateTime hour = now.minute();
  DateTime now = rtc.now();
  uint8_t hour = now.hour();
  uint8_t minute = now.minute();
  int hreading = digitalRead(Hourbutton);
  int mreading = digitalRead(Minutebutton);
    ///////////////////////////////////////////////////////////////Formula to display Hours
    digits[1]= ((hour/10)%10);//formula for the first digit
    digits[2]= (hour%10);//formula for the second digit
    //////////////////////////////////////////////////////////////Formua to display Minutes
    digits[3]= ((minute/10)%10);//formula for the third digit
    digits[4]= (minute%10);//formula for the fourth digit
   if (hreading != lasthbstate) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (hreading != hbstate) {
      hbstate = hreading;
      if (hbstate == 1) {
        if (hour == 24){
          hour = 0;
          rtc.adjust(now.hour());
        }
        else {
          hour++;
          rtc.adjust(now.hour());
        }
      }
    }
  }
  if (mreading != lastmbstate) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (mreading != mbstate) {
      mbstate = mreading;
      if (mbstate == 1) {
        if (minute == 60){
          minute = 0;
          rtc.adjust(now.minute());
        }
        else {
          minute++;
          rtc.adjust(now.minute());
        }
      }
    }
  }
  // save the reading. Next time through the loop, it'll be the lastButtonState:
    digitalWrite(STCP_pin, LOW);
        //The lines of code below is the shiftout command
        //use least LSBFIRST instead of MSBFIRST
        shiftOut(DS_pin, SHCP_pin, LSBFIRST, numbers[digits[4]]); // shift in the digit, last digit.
        shiftOut(DS_pin, SHCP_pin, LSBFIRST, numbers[digits[3]]); // shift in the digit, third digit.
        shiftOut(DS_pin, SHCP_pin, LSBFIRST, numbers[digits[2]]); // shift in the digit, second digit.
        shiftOut(DS_pin, SHCP_pin, LSBFIRST, numbers[digits[1]]); // shift in the digit, first digit.
        //The sequenece is important
    digitalWrite(STCP_pin, HIGH); // flash the digit in the correct place
        delay (100);
   
}

Please post your updated code

I updated my comment.

What does the code I posted in post 20 do?

Uh so it started counting from 00:00 for some reason.

Probably because your previous sketch overwrote the rtc modules time. Please reset the module using the current date and time manually, then run the code I posted again.

You should have the example sketch that will set the rtc modules time, however you may need to update it from the example date.

It's strange...

I physically reset the RTC by cutting all power to it, and upload one of the DS3231 sketch to rest the time, yet it still count weirdly when upload your sketch. This time it counts from 00:06.

Try this, also not tested but should be much better.

#include <RTClib.h>

RTC_DS3231 rtc;

constexpr uint8_t HOUR_BUTTON = 8;
constexpr uint8_t MINUTE_BUTTON = 9;

constexpr uint8_t DATA_PIN = 4;
constexpr uint8_t LATCH_PIN = 5;
constexpr uint8_t CLOCK_PIN = 6;

const uint8_t segmentMap[10] = { 126, 48, 109, 121, 51, 91, 95, 112, 127, 123 };

uint8_t digits[4];

bool lastHourButton = LOW;
bool lastMinuteButton = LOW;

void updateDisplay(const uint8_t hour, const uint8_t minute) {
  digits[0] = hour / 10;
  digits[1] = hour % 10;

  digits[2] = minute / 10;
  digits[3] = minute % 10;

  digitalWrite(LATCH_PIN, LOW);

  shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, segmentMap[digits[3]]);
  shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, segmentMap[digits[2]]);
  shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, segmentMap[digits[1]]);
  shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, segmentMap[digits[0]]);

  digitalWrite(LATCH_PIN, HIGH);
}

void setup() {
  Serial.begin(115200);

  pinMode(HOUR_BUTTON, INPUT);
  pinMode(MINUTE_BUTTON, INPUT);

  pinMode(DATA_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);

  if (!rtc.begin()) {
    Serial.println("RTC not found");

    while (true)
      delay(100);
  }

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  DateTime now = rtc.now();

  uint8_t hour = now.hour();
  uint8_t minute = now.minute();

  bool hourPressed = digitalRead(HOUR_BUTTON);
  bool minutePressed = digitalRead(MINUTE_BUTTON);

  if (hourPressed && !lastHourButton) {
    hour = (hour + 1) % 24; // Not sure if you wanted 12 or 24
    rtc.adjust(DateTime(now.year(), now.month(), now.day(), hour, minute, now.second()));
  }

  if (minutePressed && !lastMinuteButton) {
    minute = (minute + 1) % 60;
    rtc.adjust(DateTime(now.year(), now.month(), now.day(), hour, minute, now.second()));
  }

  lastHourButton = hourPressed;
  lastMinuteButton = minutePressed;

  updateDisplay(hour, minute);

  delay(50);
}

There are a few button libraries you can choose from that will give you actual debounce instead of adding in a blocking delay at the end. You had it at 100 (milliseconds) but that is a bit too long so I made it 50 instead.

Btw, do your buttons have pulldown resistors? I noticed you are setting the button pins to INPUT instead of INPUT_PULLUP. If you are not using any resistors, then without setting the pins as INPUT_PULLUP, you will see some weird results as the pins are considered "floating".

Holy cow it worked! It is this simple? Idk where I went wrong with my version.