How to: do something depending of time?


I would like to measure different parameters (e.g. Temperature, humidity,...) every 15 minutes by different sensors connected to arduino duemilanove.

Since the ds1307 RTC that i have i can´t manage alarms, i should need to read the time and when the minutes is 0, 15, 30 and 45, the arduino should read the sensors.

Now i am measuring every 15 minutes (plus minus), by the use of millis, but the time of the measurement is related to when i reset the arduino.

So, my question is how to do that: read different sensors at specific time coming for a DS1307. I would like to use the arduino with batteries, so as less energetic could be the procedure, more energy i could save.

The pseudo-code that i have on mind is something like that:

void loop() read the time if minutes is different than 0, 15, 30 or 45, blink a led else read temperature read humidity read other sensors save data into a microSD wait for 15 minutes... or start the loop again waiting for that time

any idea will be really welcome!! Thanks so much!

today I have found something very useful about millis() which is a way to reset it.

//declare this in the scope of vars:

extern volatile unsigned long timer0_millis;

//then attach this function to your code:

void resetMillis()
 cli();  //this will disable interrupts
 timer0_millis = 0; //this resets millis
 sei(); //this enables interrupts

//so every time you need to reset millis, just call  resetMillis();

void startNewAlarm()

Hope it might be usefull to you too. ;)


ps. Im not an expert, so give a look in other treads about "reseting millis" .

This thread seems to say it can't be done with a 1307, but could be done with a 1337.

Good luck!

Both of the other replies tell how to do something other than what you asked about. The psuedo code you posted is exactly what you want to do, except for the last line.

The loop function runs in an endless loop, so it will loop again. The last line, therefore is unneeded.

One thing to watch out for is that the minutes value returned by the DS1307 will be 0, 15, 30, or 45 for many, many iterations through loop. You'll need a way to make sure that only once does data get collected and written each time the DS1307 returns 0, 15, 30, or 45. Reading seconds won't help, because loop will be executed many times a second.

Use a boolean value. Initially set it to false. When the DS1307 returns 1, 16, 31, or 46, set it to true. Collect and write data only when it is true and the DS1307 has returned 0, 15, 30, 0r 45. As soon as you collect and record the data, set it to false again.


I will explore you option PaulS, but also thanks to Botempos and Kitep for your ideas.

@Kitep, yes, i know that it isimposible to do by ds1307, because it not have alarms. This is way i asked about how to do something like what i show in the pseudocode, readeing the RTC and checking if the time is correct to measure.

@Botempos, thans for the idea. I will think about it in order to see if i can apply it to this or other projects.

@PaulS, i will try this option because seems to be an easy and simple solution