Hi everyone, I have a DS1307 real time clock and I’m trying to figure how to set the time using time expressed as seconds. I’m using the RTClib library.
Reverse the calculation so you get hours, minutes and seconds from the seconds value that you have. Next use rtc.adjust(DateTime(2022, 08, 08, hours, minutes, seconds)).
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
unsigned long myUnixTime = 1659916800; //Time in seconds from 1/1/1970 to today 0 h
unsigned long mytimeSeg = (1234); //(time from 0 h to now in seg )
//rtc.adjust(DateTime(2022, 8, 8, 0, 0, 0));
myUnixTime = myUnixTime + (mytimeSeg); //(time from 0 h to now in seg )
rtc.adjust(myUnixTime);
}
void loop () {
DateTime now = rtc.now();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
Serial.println();
delay(3000);
}
Ola, se seu problema foi resolvido, faça uma gentileza a todos do fórum, principalmente a quem te ajudou. Escreva [Resolvido] antes do título do seu tópico, assim se alguém pesquisar e encontrar seu tópico saberá como é a solução.
E, se foi resolvido por alguma ajuda, marque a que melhor descreve sua solução.
Hi,
if your problem was solved, do a kindness to everyone on the forum, especially to those who helped you.
Write [Solved] before your topic title, so if someone searches and finds your topic, they'll know what the solution looks like.
And if it was solved by some help, please tick the one that best describes your solution.