Opto22 Scheduling Subroutine

Opto22 SNAP PAC PLCs don’t have any built-in scheduling functionality.  Since I use these controllers for some HVAC work, this becomes an issue.  I put together a subroutine that can handle reoccurring daily scheduling along with special event scheduling.  It also can be used as an astronomical clock scheduler for lighting.

SchedulingThis scheduling subroutine will handle start date and end dates, start time and end times, days of the week, and specific weeks of the month.  I decided early on to store the data in strings rather than having separate integers for each field of data.  This way I can have a single string array that holds all my schedules rather than 6 or 7 integer arrays.  The downside of this approach is that the string layout would need to be rigidly defined and it would need to be parsed by the strategy.  Rolling it all into a subroutine takes a lot of the complexity out of it.

The core part of the scheduler is the scheduling string which stores the fields determining when a schedule should be active or not. Here is how I lay out a schedule in a string:

[Start Date {YYYYMMDD}] [End Date {YYYYMMDD}] [Start Time {HHMM}] [End Time {HHMM}] [WeekDays {0000000}] [Frequency {0-6}]

Start Date and End Date – The schedule will only be enabled between these dates.
Start Time and End Time – The schedule will only be on between these times.  I also used a special convention for astronomical time.  I replace the HH with a special astronomical clock code.  The first digit represents sunrise or sunset – 8 for sunrise and 9 for sunset.  The second digit represents the direction of the offset if desired – 0 for positive offset and 1 for negative offset.  The last two digits are the offset time in minutes.  So entering a start time of 9115 would be to turn the schedule on 15 minutes before sunset.
WeekDays – The days of the week the schedule is allowed to run starting with Sunday.  A 1 means the schedule is enabled for that day, a 0 means it is not. So 0111110 means the schedule runs for every weekday, but is off on weekends.
Frequency – This has 7 possible values. 0=Every week (most common), 1=1st week, 2=2nd week, 3=3rd week, 4=4th week, 5=5th week, 6=Last week of the month.

Here are some examples:

“20150101 99991231 0800 1700 0111110 0”  Run every weekday from 8am to 5pm
“20150101 99991231 9110 0200 1111111 0” Run every day from 10 minutes prior to sunset to 2am the next day*
“20150201 20150201 1400 1500 1111111 0” Run just on Feb. 1 2015 from 2pm to 3pm
“20150101 99991231 1730 1800 100000 3” Run from 5:30pm to 6pm every 3rd Sunday

*Running schedules through midnight should work as expected as long as the weekdays field is set for every day of the week and the frequency field is set to 0.  The Optoscript would need modification to work correctly if this is different.

The CheckSchedule subroutine

CheckSchedule subroutine and it's parameters

The subroutine takes four parameters.  The sSchedule is the schedule string defined above.  The nActive is the return value, 1 for an active schedule and 0 for an inactive schedule.  The nSunset and nSunrise are the seconds since midnight for sunset and sunrise which are obtained from a different subroutine.  If the astronomical clock is not being used, then nSunset and nSunrise can be set to 0 or 1,245,883 if you desire.

CheckSchedule should be called once each minute in your strategy and nActive can then be used to turn on or off your IO points or used to do some other process.

I made a sample strategy that you can download below.  It includes the CheckSchedule subroutine and the GetSunsetSunrise astronomical clock subroutine.  Instructions for using the example are included on the power up chart.

Download the subroutine and example strategy (PAC Control Basic 9.3)


1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s