Hello, I'm having trouble just updating the time on the DS3231 RTC. I'm using the library DS3232_Simple and when I run the example to set the time or read the time it always sets it to "2002-00-176T00:00:03"
the code is:
#include <DS3231_Simple.h>
DS3231_Simple Clock;
void setup() {
Serial.begin(9600);
Clock.begin();
Serial.println();
Serial.println();
}
void loop()
{
// Create a variable to hold the data
DateTime MyTimestamp;
// Load it with the date and time you want to set, for example
// Saturday the 3rd of October 2020 at 14:17 and 33 Seconds...
MyTimestamp.Day = 3;
MyTimestamp.Month = 10;
MyTimestamp.Year = 20;
MyTimestamp.Hour = 14;
MyTimestamp.Minute = 17;
MyTimestamp.Second = 33;
// Then write it to the clock
Clock.write(MyTimestamp);
// And use it, we will read it back for example...
Serial.print("The time has been set to: ");
Clock.printTo(Serial);
Serial.println();
// Remember, once you set the time, the clock remembers it and keeps
// running even if you reset or turn off your Arduino, as long as
// the clock has battery power.
Serial.print("End Of Program (RESET to run again)");
while(1);
}
but the output is:
The time has been set to: 2002-00-176T00:00:03
End Of Program (RESET to run again)
I haven't done much else since I'm new to using the Arduino, please let me know if the issue is just that I missed a step or that I'm not doing something right.
do I just move this out of the loop? I tried to move this line to the setup function but now every time the loop function calls the variable theres an error. Sorry I'm still new to this
#include <DS3231_Simple.h>
DS3231_Simple Clock;
void setup() {
Serial.begin(9600);
Clock.begin();
// Create a variable to hold the data
DateTime MyTimestamp;
// Load it with the date and time you want to set, for example
// Saturday the 3rd of October 2020 at 14:17 and 33 Seconds...
MyTimestamp.Day = 3;
MyTimestamp.Month = 10;
MyTimestamp.Year = 20;
MyTimestamp.Hour = 14;
MyTimestamp.Minute = 17;
MyTimestamp.Second = 33;
// Then write it to the clock
Clock.write(MyTimestamp);
}
void loop() {
// And use it, we will read it back for example...
Serial.print("The time has been set to: ");
Clock.printTo(Serial);
Serial.println();
}
This needs to be moved to outside of and before setup()
Just to explain, if it is in setup(), then the MyTimestamp object may not be visible to the other functions
#include <DS3231_Simple.h>
DS3231_Simple Clock;
DateTime MyTimestamp;
void setup() {
Serial.begin(9600);
Clock.begin();
Serial.println();
Serial.println();
}
void loop()
{
// Create a variable to hold the data
// Load it with the date and time you want to set, for example
// Saturday the 3rd of October 2020 at 14:17 and 33 Seconds...
MyTimestamp.Day = 3;
MyTimestamp.Month = 10;
MyTimestamp.Year = 20;
MyTimestamp.Hour = 14;
MyTimestamp.Minute = 17;
MyTimestamp.Second = 33;
// Then write it to the clock
Clock.write(MyTimestamp);
// And use it, we will read it back for example...
Serial.print("The time has been set to: ");
Clock.printTo(Serial);
Serial.println();
// Remember, once you set the time, the clock remembers it and keeps
// running even if you reset or turn off your Arduino, as long as
// the clock has battery power.
Serial.print("End Of Program (RESET to run again)");
while(1);
}
You are still not setting the time in setup() and you only print it once in loop() before entering an endless while loop. Is that what you meant to do ?
Well I'm just trying to reset the DS3231 to the right date and time. Once it's set I plan to use it on a different code. The original code I posted was just the example code from the library thats meant to set the time and date