Go Down

Topic: Looking for 5 minutes - the next nice round 5 minutes. (Read 663 times) previous topic - next topic



I am looking to use the DS3231 I2C RTC for a data logging project and I have been trying and failing to find a nice way to find out when the next nice round 5 Minute time value is reached....

The RTC can give total seconds since the begining of time itself (ie year 2000) with now.get()
Code: [Select]
2012/12/6 18:26:11 Fri
Seconds since 1/1/2000: 408133571

I am hoping to find the next round 5 minute time value and start logging from then:

So the time I am interested in, relative to the time above, would be 18:30:00.

Any idea how I would calculate a new 'rounded seconds count value' (ie the 408133571 number) so that I can evaluate it against the current time and then proceed when one equals the other?

long now = 408133571
long rounded = 40813xxxx ?

If now = rounded then..... do something.

Thanks for any help.

Coding Badly

Code: [Select]

const unsigned long SecondsPerMinute = 60UL;
const unsigned long RoundToInMinutes = 5UL * SecondsPerMinute;

unsigned long now = 408133571;

unsigned long rounded = ((now + (RoundToInMinutes - 1)) / RoundToInMinutes) * RoundToInMinutes;


Assuming the RTC can also return minutes and seconds you could try:
Code: [Select]

   if ((now.minutes % 5) == 0 && now.seconds == 0)
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


You can poll the rtc - not terribly efficient.

Or you can use the rtc to trigger interrupt and use the mcu to keep track of elapsed time to 5 minutes.

The 2nd approach is more efficient as it doesn't tie up the mcu.


Thanks folks - I now have 3 working choices, wonderful.  I'm just testing the 'if ((now.minutes % 5) == 0 && now.seconds == 0)' method overnight to see how we go.

I will also run a test on the interupt method - the DS3231 can generate an interupt every minute so I guess I could simply check the time at the point of interrupt with 'if ((now.minutes % 5) == 0 {LogData()};' which would give logged data every 5th interupt.

...now, one of those questions - Arduino has absolutely nothing to do whilst it waits for the next interupt so.... what is the best way to do absolutely nothing.... whilst waiting for a minute to go by ?  :-)

Go Up