A sensor system I'm building is required to sample every round minute
In order to achieve this, I use the following while loop:
FlagInterval = 0;
DateTime now = rtc.now();
while (FlagInterval==0){
DateTime now = rtc.now();
secq = now.second();
minq = now.minute();
hourq = now.hour();
if ( secq == 0 ) {
FlagInterval = 1;
}
}
delay(100);
dayq = now.day();
monthq = now.month();
yearq = now.year();
//sensors loop begin
most of the time, the loop works good.
Except for the seam between dayes - at 12 o'clock at night.
When switching between the days, on the first reading (00:00:00) for some reason today's date remains on the previous day and does not move to the next day
It seems to me that it is not so clear, so here is an example:
(1) In the end of each day, the day is wrong:
now it is:
29/07/2024 23:59:00
29/07/2024 0:00:00
30/07/2024 0:01:00
should be:
29/07/2024 23:59:00
30/07/2024 0:00:00
30/07/2024 0:01:00
Why not move these
dayq = now.day();
monthq = now.month();
yearq = now.year();
in with these
secq = now.second();
minq = now.minute();
hourq = now.hour();
and see if that fixes the problem?
Use the "State Change Detection" example from the Arduino IDE as a guide. Do the action when now.second() BECOMES zero.
@elyasaff you don't need to block your code with while or delay
if you want to do something once a minute, just check when the minute has changed.
Pseudo-Code - as you have not provided a full example:
static int previousMinute = 69; // stores the minute the function was called last time
DateTime now = rtc.now();
int currentMinute = now.minute();
if (currentMinute != previousMinute) {
previousMinute = currentMinute;
Serial.println(F("minute has changed now do some fancy stuff here"));
//
//
//
}
even a simple "every 60000 milliseconds" based on "Blink Without delay" will work in your project.
static uint32_t previousMillis = 69; // stores the millisecond timestamp the function was called last
uint32_t currentMillis = millis();
if (currentMillis - previousMillis > 60000) {
previousMillis = currentMillis;
Serial.println(F("millis() has passed one minute - now do some fancy stuff here"));
//
//
//
}