Updating DS3231 in the body of the code ..

I have a small clock that uses the DS3231 RTC and have noticed that the time drifts (up to 5 seconds per day) - so I thought I would capture 6 seconds past midday and set the clock back to 1 second (keeping all the other parameters. However, I can’t set up the setDateTime dt() to work.

Any help would be most welcome.

The code goes like …

#include <Arduino.h>
#include <Wire.h>
#include <MicroLCD.h>
#include "Sodaq_DS3231.h"
#include "DHT.h"

#define DHTPIN 2 
#define ledPin 13
#define PIR 3

char weekDay[][9] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
LCD_SH1106 lcd; /* for SH1106 OLED module */
#define DHTTYPE DHT22   // DHT 22 
 boolean Screen = true; // screen is by default - on

void setup()

  DateTime dt();
   pinMode(PIR, INPUT);

void loop()
  DateTime now = rtc.now();
  if ((now.hour() > 20 and now.hour() <= 23)  or (now.hour() >= 0 and now.hour()< 7)) {
    if (Screen) {lcd.clear();}
    Screen= false; // screen is now off
    int val = digitalRead(PIR);   // read the input pin
    if (val == HIGH){ 
       delay (8000);
  else {
    Screen = true; // during the day
 if (Screen) {
  float Sec = now.second();
  if (Sec == 0){ 

  if(now.hour()==12 && now.minute()==0 && now.second()==6) {
  // set the clock back 5 seconds
  digitalWrite(LED_BUILTIN, HIGH); // led shows we have captured 6 seconds past noon
   dt()= (now.year(),now.month(),now.date(),now.hour(), 1, now.dayOfWeek());
  //   rtc.setDateTime dt;
  void display()
  DateTime now = rtc.now();  
  float Hour = now.hour();
  float Minute =now.minute();
  float Sec = now.second();
  if (Hour > 12){Hour = Hour -12;}
  if (Minute < 10){lcd.printLong(0);}
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  lcd.printLong(h - 12);
  lcd.print(" rH ");
  lcd.println(" oC");

Without bothering to look at your code, your problem seems unreasonable, and so does your solution.
There are two RTCs popular among Arduinho users:

the DS1307, which is not famous for its accuracy
the DS3231, which is.

Your DS3231 is performing unreasonably badly, even by DS1307 standards. Are you sure it's a DS3231? If it's a DS1307, you could upgrade to a DS3231 for about $3 and properly put the problem out of the way instead of messing about with code, probably with no end.

If it really is a DS3231, there could be something else going on.

If it really is a DS3231 and your code really is kosher, you might get suss about the module, and the same $3 solution applies.

It's a real ds3231 - has anyone else looked at it's long-term drift?

It's a real ds3231 - has anyone else looked at it's long-term drift?

If it drifts that badly, it is either a fake, or you are doing something wrong.

Many, many people have examined the long term drift of DS3231 modules, including the cheap ones you can buy on eBay. Expect a genuine DS3231 to keep time to within two minutes per year.

Some tests and lots of other useful info here: Using a $1 DS3231 Real-time Clock Module with Arduino | Underwater Arduino Data Loggers

What an impressive body of work he has done. Thanks for the link - I don't think I'll do any time correction. Cheers ...