Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: mhw on April 01, 2014, 07:49:07 AM

Title: Timed Event
Post by: mhw on April 01, 2014, 07:49:07 AM
I often include an output for a lighting circuit in my projects. I provide a set point for the on time and one for the off time. This appears on the surface to be a very simple task. However I have never been able to make it happen to my satisfaction. Does anyone have some good code that they want to share? I'd love to see it.
Thanks,
Mike
Title: Re: Timed Event
Post by: LWgreys on April 01, 2014, 10:43:35 PM
Not sure if it's a flashing light you want to turn ON and OFF. If it is then I use ST4 contact which is the one second timer contact. It's On for a half second then it's off a half second continuously. No other timers needed. If I want the same light to stay on, I parallel another contact to it.
Title: Re: Timed Event
Post by: mhw on April 02, 2014, 07:02:48 AM
Quote
Not sure if it's a flashing light
No, Think of a dusk to dawn light.
Title: Re: Timed Event
Post by: ATU on April 02, 2014, 08:07:25 AM
Date and time are stored in a structure, so you can keep all of those variables together as an entity.  Have you looked at the DTCMP instruction? 
Title: Re: Timed Event
Post by: mhw on April 02, 2014, 09:25:19 AM
I do not do a very good job explaining what I mean. Attached is a sample of what I have. The goal here is to turn the lights on at a preset time every night and then off at a preset time. What I have seems awkward. I thought maybe someone had done this and had a better way.
Title: Re: Timed Event
Post by: franji1 on April 02, 2014, 09:52:15 AM
I wrote it using a single rung.  Given the "OnHour" and the "OffHour", there are 3 situations that the comparisons for turning on the output are different.
1. When OnHour > OffHour, e.g. turn on at 8PM (OnHour is 20) and turn off at 8AM (OffHour is 8)
2. When OnHour < OffHour, e.g. turn on at 9AM (OnHour is 9) and turn off at 5PM (OffHour is 17)
3. When OnHour = OffHour, e.g. just turn it on during the "noon hour" from 12:00 to 12:59 (OnHour is 12, OffHour is 12)

See the attached rung.
Title: Re: Timed Event
Post by: mhw on April 02, 2014, 10:36:58 AM
That certainly is much simpler and I like it. But you did not include minutes.
Title: Re: Timed Event
Post by: franji1 on April 02, 2014, 10:43:52 AM
Rather than complicate the ladder logic with more complex .Hours and .Minutes relational contacts, generate a "day-minute" value from all Hour and Minute values.

D0: OnHour
D1: OnMinute

D3: OffHour
D4: OffMinute

Use the formula:
DayMinute = (Hour * 60) + Minute

so
D2: OnDayMinute = (D0 * 60) + D1
D5: OffDayMinute = (D3 * 60) + D4

D6: NowDayMinute = ($Now.Hour * 60) + $Now.Minute

So now you just replace in my logic OnHour with OnDayMinute, OffHour with OffDayMinute, $Now.Hour with NowDayMinute

You can do all those DayMinute calculations in a bunch of MATH boxes in a rung just before the OUT coil logic rung.
Title: Re: Timed Event
Post by: mhw on April 02, 2014, 11:23:50 AM
Thanks, Looks great!