Time of day storage

Hi. I have read articles about not requiring hardware to store day and time on Arduino sketch.
https://playground.arduino.cc/Code/DateTime
Library's for date and time

But unsure of usage.

If I had a usage to turn certain LEDs off at 10PM and back on at 9AM
Is this doable without need Arduino having external hardware, being plugged into computer and being reset / powered off on

I have a system with lighting effects and at night would be nice to turn them off and back on in the morning..

Can anyone tell me about this?

Thanks

toxsickcity:
If I had a usage to turn certain LEDs off at 10PM and back on at 9AM
Is this doable without need Arduino having external hardware, being plugged into computer and being reset / powered off on

No.

Without external hardware (e.g. DS1302) the basic Arduino cannot remember the time when it is powered off.

Using this Time Library, most arduinos can be told the current time and then keep counting time while it is powered on.

The Arduino Zero is different, it has a built-in Real Time Clock which can keep time running on a battery, see: RTC Library

Yours,
TonyWilk

The Arduino does not make an accurate clock -- can gain or lose several minutes every day and has to be reset every power up.

To keep time, get an RTC module (DS3231 is good) and use that to maintain time, even during power off. Plenty of tutorials, etc. on line.

toxsickcity:
But unsure of usage.

By itself, virtually no usage. It can be done but, if you read all the article, you will see that it is a pretty stupid idea, and no doubt you will be reminded of that fact every time you turn the power on. The advantage of using "hardware" i.e. an RTC is that you don't have to reset the time whenever the power is turned on and, you will probably conclude that paying about $3 for ds3231 is a smart move.

To digress a bit, that code it not such a bad idea if you have something like WiFi , whereby you can automatically set the "clock" on startup and check the time at midnight.

If you only need night/day detection you could use a photoresistor instead of worrying about time. Then it will automatically adapt to seasonal day length changes.

pert:
If you only need night/day detection you could use a photoresistor instead of worrying about time. Then it will automatically adapt to seasonal day length changes.

Based on that you can make a circadean clock. Assumption is that it will be midday in solar time halfway between dawn and dusk when the photoresistor toggles. By adjusting this midday slowly, it becomes slightly more precise. Note that couldy morning and open sky in the evening "corrupts" your time so adjusting in small steps is important.

Then by resetting your internal clockreference to (solar) midday, millis() can tell you when it is 3:14:15 PM etc. in solar time off course. By applying the offset between the "real" time and your solar time (your lattitude helps you), you can get a reasonable time of day. BUt it might take a week or two to stabilize.

Over the hear the midday point (as in halfway timewise between sunset and sunrise) changes. You can see that around 21/12 and 21/6. In December, sunset becomes later while the days are still shortening, as sunrise becomes later, too.

At my latitude, this year on 7/12 sunrise is 6:50 and sunset is 17:39, while on 30/12 it is 07:02 and 17:49. Likewise the sun transit time goes from 12:15 to 12:26 over that period.

How accurate do you need it to be? And how often will it be losing power?

Yes, if you use the system clock and just multiply that by the number of seconds in a day, it's not super accurate. But it is pretty consistent, so if you actually measure a day you can adjust the delays and get it to be more accurate.

Yes, you have to set the time every time you power up. But you can easily fix that so you just power it up at a set time. Say you get up at 6:00 AM, you go power on your arduino first thing and it knows to turn on the lighting effects in two hours. That sort of thing.

Just wondering why you have your lighting effects on in the daytime and off at night?

How about using an eternal power supply for your system? The Arduino and associated items (excluding the lights you are turning on and off) will not draw much, then no need to worry about being connected to a PC.

wvmarle:
Over the hear the midday point (as in halfway timewise between sunset and sunrise) changes. You can see that around 21/12 and 21/6. In December, sunset becomes later while the days are still shortening, as sunrise becomes later, too.

At my latitude, this year on 7/12 sunrise is 6:50 and sunset is 17:39, while on 30/12 it is 07:02 and 17:49. Likewise the sun transit time goes from 12:15 to 12:26 over that period.

Very true,
So that means on average it is 12:20 on midday so I would expect you are 20 minutes = 5 degrees from a meridian. Right?

Recall that the oldest clocks only had an hour hand...

Thank you all so much for your replies.. they have been educational.. I have indeed gone the way of a photoresistor and configured that to activate a relay that shuts all lighting off!

Thanks again Arduino.cc

Geez. Buy an rtc. They are super cheap and easy. I have a program that uses both a rtc and a photocell. Basically- I want to turn on a light at dusk... and have it run for 3 hrs. So... I tell the rtc to start checking the photocell at 4pm -then look for the right level of dark—light on... then, don’t check photocell again until next day at 4pm.

robtillaart:
Very true,
So that means on average it is 12:20 on midday so I would expect you are 20 minutes = 5 degrees from a meridian. Right?

I'm at 114 E, 22 N. Not sure where the meridians are exactly.

Recall that the oldest clocks only had an hour hand...

Indeed. That's the accuracy they could get. Back then there were no time zones and so.

if you did want to tell the time, you could actually make a "digital sundial" !

either with an array of photoresistors, or a moving arm that arcs the expected position of 'needle point (shadow)' at different times of the day of the year (and location !).

all the optional ways aside,
there are more than a few ways to do time.

first, if you really do not care if you are off for a few minutes each day, like water the lawn for 20 minutes, every 3 days. and over the summer it just gets wattered a few minutes later (or earlier) each day, then use millis() and bob's your uncle.

if you want a time clock to sound the school or work bell at 9:00:00 every single day, then you can use a RTC as a $1 add-on card, or get an internet connection and re-set your time a few times a day.

if you want to get off the grid.....
and you know your longitiude and latitude, you can create a 365 day lookup table with solar progression/regression.
say, monday is a bright sunny day, your table says that at 6:32:41 is sunrise and 7:02:47 is sunset. your light sensors trip at 6:32:44 and at 7:01:23
you work out the difference because your light sensor is not perfect, and then plug that into your sketch. you monitor your daily drift as you re-set daily, then a cloudy day comes and at 11:04:16 you get 'sunrise' you know your sensor is off, so you use your drift factor and work as if things were normal. the next clear sunrise that is close will give you the correction back.

since the RTC is on an I2C bus, and you can use multiple sensors on that bus, it does not (may not) use any extra pins.

since there are more than a few on e-bay under $1, your choices are not expensive in dollars.

dave-in-nj:
if you want to get off the grid.....
and you know your longitiude and latitude, you can create a 365 day lookup table with solar progression/regression.
say, monday is a bright sunny day, your table says that at 6:32:41 is sunrise and 7:02:47 is sunset.

It's far easier than that, just use a solar position library with an RTC: