Timing and LED control

Hello all! It’s been a while since I’ve posted here but you’re always my go to when in need. So I’ve got a fairly detailed project but at the moment the issue I’m having is as simple as this exercise:

On an arduino mega I would like to be able to:

const int lowestPin = 22;
const int highestPin = 28;

void setup()
{
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++)
{
pinMode(thisPin, OUTPUT);
}
}

void loop()
{
for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++)
{
digitalWrite(thisPin, HIGH);
delay(86400000);//You may notice this is the number of milliseconds in a day
digitalWrite(thisPin, LOW);
}
//delay(10);
}

Where the delay is set to 24hours I would like to use any of the following methods:
Wifi
Ethernet
GPS

to determine time of day so that the idea being pin 1 is applied power on a monday, pin 2 on tuesday, etc.

Thoughts, ideas? I’m not sure which route to go and would love some advice.

Thanks all!

Ray

Delay is an awful way to do that. How about this instead:

void loop(){ if (millis()>=nextDay){ dayofweek = dayofweek+1; // turn on LED for day of week & turn off prior day nextDay = nextDay + 86400000UL; } // use wifi, whatever to do any time of day reporting needed while waiting for the next 24 hour period to pass }

Cool! I appreciate the education on that part(using your method rather than delay). The only problem is I cannot assume the system will be turned on at 12 midnight. I have to account for power outages(system reset) and for users(not myself) resetting the system at any given time, say 5pm.

Because of needing to account for that case I thought teaching the Arduino the actual time would be helpful. I guess the alternative could be to build a battery backup system so I could set the system and it would always be right. This is in a mission critical situation where it cannot afford to be wrong(non life threatening consequences).

Thanks for the quick reply!

Sure, add battery backed RTC chip like DS1307 which has battery back memory also, or later versions of same that have internal crystals, for time keeping. Then just ping it once a second to see if its time to do something, use its memory to hold the next time for an update that you are tracking to.

http://www.maxim-ic.com/datasheet/index.mvp/id/2688/t/al http://www.maxim-ic.com/datasheet/index.mvp/id/4051 http://www.maxim-ic.com/datasheet/index.mvp/id/4627

I saw that but the line "It may lose or gain up to 2 seconds a day." scared me, it could be off by up to 12 hours at the end of the year and that wouldn't work but its big brother http://www.adafruit.com/products/255 apparently would do just fine. Any experience with that by chance?

"Completely Integrated, Accurate Timing Solution Provides Better than ±2.0 Minutes per Year Accuracy Over the Industrial Temperature Range and Requires No User Calibration"

2 minutes a year be okay? That's why I suggested the parts with internal crystals.

Yup, 2 minutes a year will be ok. The others you suggested were great but they looked like they'd start to bring the project cost up a bit but more so they had example arduino integration over the others. My confusion is that your DS3234 at http://www.sparkfun.com/products/10160 on sparkfun stipulates it has months and years(which I'll need since I need to know the day of the week, although on a separate note how would I be able to determine day of the week?) the Chronodot at http://www.adafruit.com/products/255 only speaks to hours and minutes. Thoughts on how to determine day of the week(going back to state 1 is monday, state 2 is tuesday).

Your help this evening is immensely appreciated!

The ChronoDot/DS3231/DS3234/DS1307 all have the same timing registers. Second, minute, hour, day of week, day of month, month, year, are all available. Any example you find for the DS1307, except for NVRAM uses, will work on the ChronoDot. The DS3234 uses a different interface, so you can't directly use DS1307 code, but it'll be much the same.

If you have the ChronoDot in a room temperature environment, people find that the accuracy is even better than quoted over the full industrial range. I have a ChronoDot hooked up to my server, just checked it now; it was last synchronized with NTP on March 8th of this year. As of now, it has only lost 2 seconds.