How can I Resetting data every 24 hours using RTC

Hi Guys,

I am working on a weather station and I am reading alot of sensor values like rainfall, wind speed, direction, temperature,Pressure, and so on.

Few data such as daily rain count and max wind speed in a day, I would like to reset this values to 0 at every midnight (24Hrs).

I am using the Adafruit data logger shield [ Adafruit Assembled Data Logging shield for Arduino : ID 1141 : $13.95 : Adafruit Industries, Unique & fun DIY electronics and kits ] with the RTC.

I have written a function, however it seems if I call the function in the main loop, which has few delays, the function does not work all the time. I think it is because my delays add up to a odd number.

Can you please help me to figure out how can I reset my data every 24hours.

String reset()
{
  DateTime now = rtc.now();
  String data;
  if ((now.hour() == 11) && (now.minute()==19)&& now.second()==0)
   {
    data= ("daily data have being reset");
   }
    else
    {
     data=("could not reset");
   }
   return data;
}
  • Don’t use the String class

  • So you know you have weird delays and suspect this is why your code does not work and yet you post some code that is unrelated and does not show the delays? How can we really know what’s going on? don’t post snippets, don’t assume the bug is where you think it is..

  • your test against a specific time means you need to be there at the exact second - indeed if your delays and execution time add up to more than 1 sec and you calls this once in a loop you might miss the right time...

An idea: check when you have changed day (ie remember the previous day that was used to reset the data compare with current day - if they differ and this is when you reset your data (and update previous day value). That will give you a window of 24h to reset the counter... should be enough even if your loop() is slow - it should not be THAT slow.. (I hope)

J-M-L:
An idea: check when you have changed day (ie remember the previous day that was used to reset the data compare with current day - if they differ and this is when you reset your data (and update previous day value). That will give you a window of 24h to reset the counter... should be enough even if your loop() is slow - it should not be THAT slow.. (I hope)

Thanks alot J-M-L, Sorry for not posting the main loop delays, I actually keep changing them since im still working on the code.

I will try what you have suggested with using the days for reset. The RTC module does give you the number of days up since 1/1/1970.

The reset may not be exactly at 00:00:00, however it does not need to be exactly at midnight, as long as it gets reset at 24hour interval.

Thank you for your Help and time. Much appreciated.

Regards,
Vishal.

V1shal:
I will try what you have suggested with using the days for reset. The RTC module does give you the number of days up since 1/1/1970.

Well I see you doDateTime now = rtc.now();I would suggest you look up what the [url=https://github.com/adafruit/RTClib/blob/82d00a1a15bcf738b001e42bf26a3d1f1a15be32/RTClib.h#L30]DateTime[/url] class has to offer ... :slight_smile:

Yes it might not be exactly midnight, that’s your current problem - not sure what your RTC is (DS3231 ?) - some can have alarm interrupts triggered at a specific moment. If you want to be super precise this could be a way.

you should fix your loop and get rid of the delays though, long loops make for poorly interactive code if you need any button press or other human interaction