Daily-action question

Hi all, i am a sculptor working with electromechanical systems for a while now, though i am just getting up to speed with the arduino and could use a little help:

I would like to have my device perform a function once per day (say, run a motor every day at 2pm). I have found the DateTime library in the Playground. Is this the best way to achieve this result, or can I set a delay to count off 24 hours?

If there is a thread already addressing this, it seems i couldn't spot it and I would be glad to be pointed in the right direction.

If your board should do nothing for 24 hours, then do one small task, then using delay() should be fine. Note that the crystal is accurate to within a few seconds of drift per day, so any inaccuracy in delay() will probably be lost in the noise.

If you want to do various tasks, and for one task, notice that 24 hours have elapsed since the last time it ran, then you should track the passage of millis(). Ignoring leap seconds and leap days, 1 day = 86,400,000 milliseconds (simple math). The millis() number will roll over at some point. I think it's 32 bits, so (2^32) milliseconds = 49.7102696 days. There's just a little math to do when you notice the millis() number returns is LESS than your last task invocation.

Perfect, thanks very much halley.

Any specific code suggestions on tracking/referencing the current millisecond count during a sketch?

the general idea is to have a variable holding the last update, and a variable/define holding the 'delay'

//#define d_second 1000
constant unsigned int cuiSecond = 1000;
unsigned long ulLastUpdate = 0;

void setup(){ ; }

void loop()
{
    if(ulLastUpdate+cuiSecond>=millis())//if true, then a second has passed since last update
    {
         //ulLastUpdate=millis(); //if you want a second between each execution start
         execute;
         ulLastUpdate=millis(); //if you want a second between this end and next start
    }
}

I would like to have my device perform a function once per day (say, run a motor every day at 2pm). I have found the DateTime library in the Playground. Is this the best way to achieve this result, or can I set a delay to count off 24 hours?

A common requirement to run tasks at specific times.
You may wish to consider using my recently introduced MRMP.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1232140631

MRMP Generic_Controller includes tasking that can be run once per minute/hour/day.

int TimeZone; // Only needed if your UNIX time is not corrected for local time.

byte hourIs()  // Returns the current hour
{
   return  ((*absolute_time_t501 % 86400) / 3600) + TimeZone;  // Days and seconds remainder
   // TimeZone Only needed if your UNIX time is not corrected for local time.
}


void taskEveryHour()
{
  // Serial.println();
  // Serial.print("HOUR Sec:");  // Debug
  // Serial.print(*time_t500);  // Debug

  //  Serial.print(" U:");  // Debug
  //  Serial.println(*absolute_time_t501);  // Debug

  Serial.print(" Hour:");  // Debug
  Serial.println(hourIs(), DEC);  // Debug

runMyMachine();   // This would check hourIs() and run if 14 (2 PM).


}

Your 2PM task could use a 300 series integer...

// int           elements 300 to 399
#define intFieldBase 300
#define intFieldSize 10
int intField[intFieldSize];

int *runAt300 = intField; //
if (hourIs == *runAt300)
{
runIt();
}

To change the hour...
Rs0vP2,300,15#

And that would run at 3PM

And if you can't wait for 3PM.

Get the UNIX time for 14:59:55
similar to... 1232377193

then use MRMP to set the time:
Rs0vA2,501, 1232377193#

Wait 5 seconds and it will run (or should), as that is what debugging is about!

Hope MRMP helps.

IMO DateTime or MRMP are overkill for this application, alphabeta's solution seems sufficient if the requirement is to simply do something every 24 hours. The other libraries are more suited for apps where something needs to be done at specified dates and times.

alphabeta's solution seems sufficient if the requirement is to simply do something every 24 hours.

Agreed.

I am not a fan of devices that rely on absolute time for their execution. I prefer to created controllers that react to external events.
I typically use absoulute time events to record running totals to EEPROM.

However in this case, I suspect 2PM was chosen because the motor may be noisy? Running it 24 hr from start or after a power failure may not be the best time to run. In fact an inexpensive Wal-Mart timer may be the best solution!

how about a digital watch with an alarm?

Take the back off the watch because the backing is usually a piezo-element speaker.

hookup the arduino to the watch (to where the piezo touched the circuit board) so that when the watch alarm goes off at 2pm, the arduino detects the voltage signal and does its daily duty.

I think dollar stores sell cheap digital watches.