Year long timer

Has anyone put together something like this. I have no problem with the electronics but the code
is out of my expertise.

john
Mims,fl

ARDUINO MULTIPLE ALARMS CLOCK
WITH 2 RELAY OUTPUT

  1. Arduino Uno
  2. RTC-DS3231
  3. LCD: LCD 1602 Module, white on blue
  4. 2 Channel relay module
  5. One external EEPROM (24LC256)

This is a sketch to output power from relay #1 for 250ms which will turn on the street lights by applying 24v dc to a latching relay. 2 1/2 hours later there will be a on command to relay #2 for 250ms which turns off the street lights.

I want to write a sketch that will turn on the street light though the year starting in January and ending in December. Each month there will be four different events, two events starting the first Wednesday, and two events on the third Wednesday. There will be three months (June, July, and August) the street lights will be turned on only on the first Wednesday (two events).

I will be polling the RTC for Date/Time (unix time) to be used with alarm time. The LCD will display the Date/Time. There will be a total of 42 different timed events in this sketch.

With the RTC having only two alarms I will have to use the body of the sketch to set the alarms. I will used unix time to do a comparison between Date/Time of day with Alarm time. All alarm times will be stored on the external EEPROM in 4 bytes unix time. When you get Date/Time equal to or greater than alarm time condition then there will be a output to turn the lights on/off.

I have two problems, 1 does the DS3231 RTC library output a unix time, and 2 if I have a power outage the sketch will start at day one (Jan.). and the lights will cycle on and off until RTC time gets close to alarm time. The street lights have a short warm up time.

2 1/2 hours later there will be a on command to relay #2 for 250ms which turns off the street lights.

Why would the on command turn the relay off? Do you turn right when someone tells you to turn left?

With the RTC having only two alarms I will have to use the body of the sketch to set the alarms.

Why do you need an alarm at all? On any given pass through loop(), it is time to do the nth task, or it isn't. If it is, do the nth task. No alarms needed.

does the DS3231 RTC library output a unix time

You ask that question like there was only one DS3231 library.

and 2 if I have a power outage the sketch will start at day one (Jan.).

That would imply that you have not set the time in the RTC. Why haven't you?

A RTC will remember the current date and time: there should be no reason why your sketch would start from January again unless you coded it.

The timelib.h library has a function to convert date/time to unix timestamp if the RTC library that you use does not have it built in.

I’m not at all clear what you’re trying to do: what’s special about Wednesdays? I’d have expected that street lighting would be useful every day but clearly there’s something else going on.

Looking at Adafruit’s fork of the Jeelabs library for the RTC library, I see that there’s a unixtime function.

The DS3231 is usually sold with a holder for a battery, so a power outage should not be an issue.

PaulS,
If you are familiar with latching relays, I am using a latching relay to control the power for the street lights (“High AC Current”) a latching relay will need two different (on different relay pins) 24 volt to turn on and off the lights. (Do you turn right when someone tells you to turn left?)??? Alarms is the terminology that I am using for a high on digital pin out of the Arduino uno.

wildbill,
I am using the Jack Christensen RTC library. The light are only used on the first and third Wednesday of the month (Wednesday is the only day someone will be there at night).
When the power goes off on the Arduino uno, when it regains power it will start at the beginning of the sketch.

john
Mims,Fl

When your sketch starts, it can look at the time & date and figure out whether or not the lights should be on so a power outage should be largely irrelevant.

Indeed, if the rules are as simple as stated, I really don't see why you need to store the timestamps, you can deduce all you need from time & date.

there is a prgression of seasons based on the Earth's tilt.
there are astronimc timers that allow for twilight and sunrise every day of the year.
this way, you are not turning your lights on at 4:30 PM in june when the sun sets after 8:00
or you are not turning off the lights at 8:00 in december when the sun sets at 4:30.

===========

as for the RTC.
that is a Real Time Clock. it works like a battery powered watch.
it runs for years on the battery.

when you turn your Arudino on, it looks at the RTC and finds out what day, month, hour, minute, second it is.
so, when you lose power, and then re-start, your Arduino looses the time, but before it exits setup() it have fetched the year/month/day/hour/minute/second and are good to go !

This is a table of the times from 05-01-2019 to 12-18-2019
john
Mims,Fl.

STREET LIGHT TIMING TABLE

DATE TIME UNIX TIME RELAY#1 RELAY#2
1 05-01-2019 19:35:00 1556739300 ON
2 05-01-2019 21:30:00 1556746200 ON
3 05-15-2019 19:30:00 1557948600 ON
4 05-15-2019 21:30:00 1557955800 ON
5 06-05-2019 19-30:00 1559763000 ON
6 06-05-2019 21:30:00 1559770200 ON
7 07-03-2019 19:15:00 1562181300 ON
8 07-03-2019 21:30:00 1562189400 ON
9 08-07-2019 19:00:00 1565204400 ON
10 08-07-2019 21:30:00 1565213400 ON
11 09-04-2019 19:00:00 1567623600 ON
12 09-04-2019 21:30:00 1567632600 ON
13 09-18-2019 18:30:00 1568831400 ON
14 09-18-2019 21:30:00 1568842200 ON
15 10-02-2019 18:00:00 1570039200 ON
16 10-02-2019 21:30:00 1570051800 ON
17 10-16-2019 18:00:00 1571248800 ON
18 10-16-2019 21:30:00 1571261400 ON
19 11-06-2019 18:00:00 1573063200 ON
20 11-06-2019 21:30:00 1573075800 ON
21 11-20-2019 18:00:00 1574272800 ON
22 11-20-2019 21:30:00 1574285400 ON
23 12-04-2019 18:00:00 1575482400 ON
24 12-04-2019 21:30:00 1575495000 ON
25 12-18-2019 18:00:00 1576692000 ON
26 12-18-2019 21:30:00 1576704600 ON

If the code is outside your experience , then you have a bit of a learning curve to get this working .
If it’s a one off , and you don’t want to get into coding , there are other ways to do this, eg “ controlbyweb” products.

It looks like the ON time is the only thing that depends on date. A simple table indexed by month would probably be close enough:

// Times to turn the light on (earliest civil sunset for the month) HHMM. 
int OnTimes[12] = {1754, 1828, 1902, 1938, 2014, 2048, 2037, 1949, 1856, 1808, 1744, 1743};
// OnMinute = OnTimes[month] % 100;
// OnHour = OnTimes[month] / 100;

What I would do is, each time the RTC minute changes, compare the current date and time to the desired on and off times to see if the light should be ON or OFF. Keep track of the state of the lamp so the relay is only used when the state has to change.

Why not use a light level sensor to turn ON near dark on the selected day and OFF every day at 21:30?

DISCLAIMER: At the risk of sounding like an ad, take a look at my controller SL4P.net/cm_index.html[\url]
I’m happy to tweak the code a bit to suit your specific requirements, or indeed, you could use a combination of time and daylight sensors to adjust the daily events.
The base controller can do what you want. The SMS capability is possibly not needed, but it does derive day/time info from the cell network.

JCA79B:
Why not use a light level sensor to turn ON near dark on the selected day and OFF every day at 21:30?

I guess because the OP wanted to use a timer.
I believe that this is a case of merging multiple technologies.
johwasseer has a good comment about using the month as the guide as the length of day. Since the change over a month (1 min 13 sec in Jun, (closer to solstice and 2 min in August closer to the equanox) and since we know the day of the year from the RTC, it would not be hard to just add (or subtract) 90 seconds per day until the next reckoning at the first of the next month. That way, instead of being over an hour off of sunset, it would not be more than 15 minutes off.
On a cloudy day, the light sensor would bring the lights on a bit early, and the timer would work if the sensor were to fail.

of course, you could just have the light sensor used for day/night and the calendar for what days of the year.

jdmac:
This is a table of the times from 05-01-2019 to 12-18-2019
john
Mims,Fl.

STREET LIGHT TIMING TABLE

DATE TIME UNIX TIME RELAY#1 RELAY#2
1 05-01-2019 19:35:00 1556739300 ON
2 05-01-2019 21:30:00 1556746200 ON
3 05-15-2019 19:30:00 1557948600 ON
4 05-15-2019 21:30:00 1557955800 ON
5 06-05-2019 19-30:00 1559763000 ON
6 06-05-2019 21:30:00 1559770200 ON
7 07-03-2019 19:15:00 1562181300 ON
8 07-03-2019 21:30:00 1562189400 ON
9 08-07-2019 19:00:00 1565204400 ON
10 08-07-2019 21:30:00 1565213400 ON
11 09-04-2019 19:00:00 1567623600 ON
12 09-04-2019 21:30:00 1567632600 ON
13 09-18-2019 18:30:00 1568831400 ON
14 09-18-2019 21:30:00 1568842200 ON
15 10-02-2019 18:00:00 1570039200 ON
16 10-02-2019 21:30:00 1570051800 ON
17 10-16-2019 18:00:00 1571248800 ON
18 10-16-2019 21:30:00 1571261400 ON
19 11-06-2019 18:00:00 1573063200 ON
20 11-06-2019 21:30:00 1573075800 ON
21 11-20-2019 18:00:00 1574272800 ON
22 11-20-2019 21:30:00 1574285400 ON
23 12-04-2019 18:00:00 1575482400 ON
24 12-04-2019 21:30:00 1575495000 ON
25 12-18-2019 18:00:00 1576692000 ON
26 12-18-2019 21:30:00 1576704600 ON

What time library are you using ?

I believe that many allow you to poll the RTC and return unix time.
as was noted by
we typically use human readable times, but only real difference is the presentation.

sterretje:
The timelib.h library has a function to convert date/time to unix timestamp if the RTC library that you use does not have it built in.

since you can get the unix time from the RTC chip and the RTC chip holds the date from when you set it, the fundamentals are there.
as a note, when you PROGRAM the RTC, you include the functions in setup() to SET the date and time a minute in advance
Then upload that to the RTC
Then you use your watch or some clock and at the time you set in the sketch, you reset your Arduino.
this sets the time in the RTC
you immediately go back to your sketch and remark out the functions that will set the date.
then you upload that.
it is this second upload that prevents the Arudino from re-setting the date with every power on cycle.

it is this 2-step process that makes the RTC independent from the Arduino.

whatever library you use, these instructions may be a bit different.