Run loop every x hours

retail power timer + plug pack
Set to turn Arduino on for 20 secs every 6 hours.

Save power, unless you want the micro to do something else for the other. 21,958 seconds !

lastchancename:
retail power timer + plug pack
Set to turn Arduino on for 20 secs every 6 hours.

Save power, unless you want the micro to do something else for the other. 21,958 seconds !

I´m sorry, but I did not understand any of that.
Power Timer + plug pack?

LINK ->PLUG PACK

LINK ->MAINS TIMER

Sorry my throw-away maths was wrong...

  • 6-hours = 21,600 seconds
  • ON / run the arduino for 20 seconds
  • OFF for 21,580 seconds

lastchancename:
LINK ->PLUG PACK

LINK ->MAINS TIMER

Sorry my throw-away maths was wrong...

  • 6-hours = 21,600 seconds
  • ON / run the arduino for 20 seconds
  • OFF for 21,580 seconds

Thanks but if I use an extern trigger, then I can use my home-Automation system the Tellstick system. Wireless 433 Mhz and Z-wave communication with good standard.

Allow me a dumb question.
Why no one ever uses the mod operator to do a cyclic operation
like
if (millis() % 3600000){ do stuff } //every 1h

Perhaps C4m4l340 meant something else. This code:

 if (millis() % 3600000){ do stuff }

will cause "do stuff" to be executed all of the time except at exactly 0 hours, exactly 1 hour, exactly 2 hours, exactly 3 hours, etc. I have not tried to figure out what happens at rollover but it can't be good.

By the way, it happens that millis() will hit 3600000 and its integer multiples, but there is no documented guarantee of this. millis() skips some values.

C4m4l340:
Allow me a dumb question. Why no one ever uses the mod operator to do a cyclic operation like

if (millis() % 3600000){ do stuff } //every 1h

Even if you get the sense right: "if ((millis() % 3600000) == 0){ do stuff }", if the loop() takes more than a millisecond to complete you could miss the interval entirely and miss an hour.

It's similar to the reason why a proper timer uses:

  if (currentMillis - previousMillis >= interval) {

Rather than:

  if (currentMillis - previousMillis == interval) {

Also, a 32 bit subtract and compare is faster than a 32 bit division (needed for a modulus operation).

Thank you for your replies.
in fact i ment if ((millis() % 3600000) == 0){ do stuff } and not if ((millis() % 3600000)){ do stuff }

It make sense, if the loop takes more than 1 millisecond to complete, it will miss completely the time.

Thank you very much

More probably the code will execute too often. Consider that loop() is called 100,000 times per second, then millis() will change only after 100 invocations.