News:

  • August 27, 2026, 04:33:28 PM

Login with username, password and session length

Author Topic: Timed Event  (Read 15493 times)

mhw

  • Hero Member
  • *****
  • Posts: 250
Timed Event
« 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

LWgreys

  • Hero Member
  • *****
  • Posts: 117
Re: Timed Event
« Reply #1 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.

mhw

  • Hero Member
  • *****
  • Posts: 250
Re: Timed Event
« Reply #2 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.

ATU

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 2126
  • YKPAIHA
    • ATU, Inc.
Re: Timed Event
« Reply #3 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? 

mhw

  • Hero Member
  • *****
  • Posts: 250
Re: Timed Event
« Reply #4 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.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3843
    • Host Engineering
Re: Timed Event
« Reply #5 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.

mhw

  • Hero Member
  • *****
  • Posts: 250
Re: Timed Event
« Reply #6 on: April 02, 2014, 10:36:58 AM »
That certainly is much simpler and I like it. But you did not include minutes.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3843
    • Host Engineering
Re: Timed Event
« Reply #7 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.
« Last Edit: April 02, 2014, 10:46:16 AM by franji1 »

mhw

  • Hero Member
  • *****
  • Posts: 250
Re: Timed Event
« Reply #8 on: April 02, 2014, 11:23:50 AM »
Thanks, Looks great!