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
DHT dht(DHTPIN, DHTTYPE);
boolean Screen = true; // screen is by default - on
void setup()
{
lcd.begin();
rtc.begin();
DateTime dt();
pinMode(PIR, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
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){
display();
delay (8000);
lcd.clear();
}
else {
Screen = true; // during the day
}
}
if (Screen) {
float Sec = now.second();
if (Sec == 0){
display();
}
}
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;}
lcd.clear();
lcd.setFontSize(FONT_SIZE_XLARGE);
lcd.setCursor(35,0);
lcd.printLong(Hour);
lcd.print(':');
if (Minute < 10){lcd.printLong(0);}
lcd.printLong(now.minute());
lcd.println();
lcd.println();
float h = dht.readHumidity();
float t = dht.readTemperature();
lcd.setFontSize(FONT_SIZE_XLARGE);
lcd.printLong(h - 12);
lcd.print(" rH ");
lcd.printLong(t);
lcd.println(" oC");
delay(2000);
}