24 hour check loop using Time.h

I need to create a 24 hour check loop. The sketch is using the Time.h library.

What I need to do is wrap a portion of code in a loop that checks to see if the time has past the 24 hour period. If it has, then it runs the code. It needs to start the 24 hour loop again and won't allow that portion of the code to be run again until the 24 hour period has elapsed.

To shorten this up. It basically just needs to run a certain part of the code every 24 hours.

I'm a beginner at programming and need some help figuring this one out. Thanks!

Hello,

You don't even need the time library, just count if enough time has passed, like this for example

void loop()
{
  static uint32_t prevTime;
  uint32_t curTime = millis();

  if ( curTime - prevTime >= 24*60*60*1000UL )
  {
    prevTime = curTime;

    // do things every 24h here
  }
}

If you need more accuracy, you will need an RTC ;)

guix: If you need more accuracy, you will need an RTC ;)

or, if you wish it to survive a power cycle...

Hello J_Dub,

your main issue is to run a certain part of the code every 24 hours, I know how it feels especially when you are new in coding. i had the same problem yesterday but some intelligent people here in this forum assist me to solve it, what you are suppose to do is to go step by step and don’t rush.

for doing so there are different alternative to use depend on the task or multask of your code. You could rather post your code we see where you have reached and you will be helped from there.

But all in all there is arguments of not using delay() because its going to hang other task but since you have not post your code let me give you clue for coding every 24 hours without using delay

codes:

//your variables are here

void setup(){

//your PinMode(); here

//your PinMode(); here

}

void loop(){

//do something here

unsigned startTime = millis();

while( millis() - startTime< (24UL60UL60UL*1000UL))

//the above “do something here” will be executed only if while( millis() - startTime< (24UL60UL60UL*1000UL)) is true.

//24UL60UL60UL1000UL means 24hrs
//so you can try for 1 minute which is 60UL
1000UL

}
}

It isn't clear from your post if you mean that you need to run the block of code once every 24 hours, or if you mean that you need to run the code at the same time each day.

How you do each is different.

PaulS: It isn't clear from your post if you mean that you need to run the block of code once every 24 hours, or if you mean that you need to run the code at the same time each day.

How you do each is different.

Once every 24 hours.

Thanks for all the suggestions! I'll try them and see if we get the results we're looking for.