Clock : Seven segment display problem [Solved]

Hi everyone,
I’m asking your help.
I have a problem with a 4 digits seven segment display. I’m trying to make a clock using an arduino mega, a ds3231 module and a seven segment display. I’m using the sevseg library to control the display and the DS3231 library by jarzebski ( GitHub - jarzebski/Arduino-DS3231: DS3231 Real-Time-Clock ) to control the rtc module.
I managed to display the time and to set it with three push buttons. With my code, when I press the button to set time, it entered a loop. Then, in this loop, if i press the Minute button it increments minutes and if I press the Hour button it increments the hours in the module.
However, when I press the minute or hour button, the display kinda display random things. In fact, it lights up random segment when I want it to switch smoothly between number.

I gave with this post my code and a screenshot of the random display too.

Thanks for any replies. :slight_smile:

(I apologize for my bad english, as I’m french)

code.pdf (70.5 KB)

I gave with this post my code

As a stupid pdf file. Try again, as a text file.

Are you able to display numbers from 0 to 9 on each of the 4 seven segment displays, with out the clock stuff?

There you go. I’ll try tomorrow

#include "SevSeg.h"

#include <Wire.h>
#include "DS3231.h"

SevSeg sevseg; //Instantiate a seven segment object
DS3231 clock;
RTCDateTime dt;
const int HButton = 2;
const int MButton = 4;
const int SetTButton = 3;

int CurrentHour = 0;
int CurrentMin = 0;
int lastButtonTState = 0;
int ButtonTState = 0;
int ButtonHState = 0;
int lastButtonHState = 0;
int ButtonMState = 0;
int lastButtonMState = 0;



void setup() {
    Wire.begin();
    pinMode(HButton, INPUT);
    pinMode(MButton, INPUT);
    pinMode(SetTButton, INPUT);

    byte numDigits = 4;
    byte digitPins[] = {52, 50, 48, 46};
    byte segmentPins[] = {38, 42, 36, 34, 32, 44, 40, 30};
    sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
    sevseg.setBrightness(50);

    clock.begin();

}

void loop() {
    sevseg.refreshDisplay();
    MiseaHeure();
    AfficherH();

  delay(1);

}

void AfficherH() {
     dt = clock.getDateTime();
    sevseg.setNumber(dt.hour*100+dt.minute, 4);
}


//Définition de la fonction mise à l'heure
void MiseaHeure() {
   CurrentHour = dt.hour;
   CurrentMin = dt.minute;
   ButtonTState = digitalRead(SetTButton);
    if (ButtonTState != lastButtonTState) {
     if (ButtonTState == HIGH) {
  while (ButtonTState == HIGH) {
       sevseg.refreshDisplay();
       ButtonHState = digitalRead(HButton);
       ButtonMState = digitalRead(MButton);
       if (ButtonHState != lastButtonHState) {
       if (ButtonHState == HIGH) {
         //If needed, we change the hour
         CurrentHour = CurrentHour + 1;
         if (CurrentHour >= 24) {
           CurrentHour = 0;
         }
    clock.setDateTime(dt.year, dt.month, dt.day, CurrentHour, dt.minute, 
dt.second);
    sevseg.refreshDisplay();
    delay(500);
       }
     }
     else if (ButtonMState != lastButtonMState) {
       if (ButtonMState == HIGH) {
         //If needed, we change the minutes
          CurrentMin = CurrentMin + 1;
         if (CurrentMin >= 60) {
           CurrentMin = 0;
         }
    clock.setDateTime(dt.year, dt.month, dt.day, dt.hour, CurrentMin, 
dt.second);
    sevseg.refreshDisplay();
    delay(500);
       }
     }
AfficherH();
sevseg.refreshDisplay();
ButtonTState = digitalRead(SetTButton);
     }
    }
  }


lastButtonTState = ButtonTState;
lastButtonHState = ButtonHState;
lastButtonMState = ButtonMState;
}

I found the problem. Instead of putting a delay (which stop the program and therefore the seven segment display), I put this code :

AfficherH(); 

for(unsigned long debut = millis(); millis() - debut < 500;) {
  sevseg.refreshDisplay();
}

So the numbers switch smoothly now.