Timer Logic

Discussion in 'Pascal Logic Code Examples' started by magic8, Jan 3, 2017.

  1. magic8

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    1
    First time trying a timer logic in wiser 2
    Entered below in wiser project and ran with no errors
    Transferred project to wiser2 unit and timer side of all logics will not work
    Anyone have any idea why not
    Probably me being stupid!!!!
    Thanks

    once (GetLightingState("Dining Room Lts") = ON)
    then timerstart(3);
    begin
    once timertime(3) > 2
    then SetLightingState("Dining Room Lts", OFF);
    begin timerstop(3);
    end;
    end
     
    magic8, Jan 3, 2017
    #1
  2. magic8

    NickD Moderator

    Joined:
    Nov 1, 2004
    Messages:
    1,420
    Likes Received:
    62
    Location:
    Adelaide
    Code:
    once (GetLightingState("Dining Room Lts") = ON)
    then timerstart(3);
    begin
    once timertime(3) > 2
    then SetLightingState("Dining Room Lts", OFF);
    begin timerstop(3);
    end;
    end
    I think you've got a few more semicolons than you need.

    The semicolon terminates the once statement so what you effectively have is

    Code:
    once (GetLightingState("Dining Room Lts") = ON)
    then timerstart(3);
    Which starts the timer when the group goes on.

    Followed by

    Code:
    once timertime(3) > 2
    then SetLightingState("Dining Room Lts", OFF);
    Which turns the group off when the timer exceeds 2 seconds.

    Followed by

    Code:
    timerstop(3);
    Which stops the timer, always.

    As a C programmer I find the optional semicolon and optional begin/end in pascal confusing so I like to always put in begins and ends and indent to make the code clearer (in my mind anyway).

    It's not entirely clear what you are intending to happen (putting comments as to what you are intending to do also help (even for yourself when you come back 5 years later and wonder what the hell you were thinking)), but if you're trying to also stop the timer at the same time as you are turning off the group then you need to put begin and end around the statements after then "then".

    Code:
    {Start the timer when the group turn on}
    once (GetLightingState("Dining Room Lts") = ON) then 
      begin
        timerstart(3)
      end;
    
    {Stop the timer and turn off the group when the timer has run for > 2 seconds}
    once (timertime(3) > 2) then 
      begin
        SetLightingState("Dining Room Lts", OFF);
        timerstop(3);
      end;
    
    Nick
     
    NickD, Jan 4, 2017
    #2
  3. magic8

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    Your begin/ends are all wrong, so the timer is stopped as soon as it starts. If you format it correctly you can see what's happening more clearly. The second ONCE only executes the setLightingState. The timerstop is outside of that ONCE.

    Code:
    once (GetLightingState("Dining Room Lts") = ON) then timerstart(3);
    
    once timertime(3) > 2 then SetLightingState("Dining Room Lts", OFF);
    begin // does nothing!
       timerstop(3);  // Executed every scan !
    end;
     
    try

    Code:
    once (GetLightingState("Dining Room Lts") = ON) then timerstart(3);
    
    once timertime(3) > 2 then 
    begin 
       SetLightingState("Dining Room Lts", OFF);
       timerstop(3);
    end
     
    or better still

    Code:
    once (GetLightingState("Dining Room Lts") = ON) then
     pulseCBusLevel("Local", "Lighting", "Dining Room Lts", 100%, "0s", "0:03:00", 0%);
    In most cases timers aren't required. I have written thousand of lines of logic and never used a timer.

    Also, you can do this in the switch without logic


    Obviously Nick types faster than me :)
     
    Last edited by a moderator: Jan 4, 2017
    Ashley, Jan 4, 2017
    #3
  4. magic8

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    1
    Thanks for replies
    Will try and see if I can get timer to work ok
    This was a simple test timer to try to explain what I was doing wrong
    The customer wants quite a complicated function involving an exhaust fan that comes on when light comes on and stays on for 1 minute after light switched off. Also if the light is left on for > 10 minutes to switch light off and fan off after 1 minute[I guess its being left on!!!!]
    Thanks again for info
     
    magic8, Jan 4, 2017
    #4
  5. magic8

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    1
    Its on an EDLT so no "blocks" to use
    Thanks
     
    magic8, Jan 4, 2017
    #5
  6. magic8

    NickD Moderator

    Joined:
    Nov 1, 2004
    Messages:
    1,420
    Likes Received:
    62
    Location:
    Adelaide
    To do that it's probably easier to use the "PulseCBusLevel" function..

    Here's a copy and paste of something similar from my own touchscreen project.. (note the fan is on a dimmer in my case).

    Code:
    { Turns fan on low when light is turned on, then sets it to 
       high for 3 minutes once light is turned off }
    
    once (GetLightingState("Toilet") = ON) then
    begin
      SetLightingLevel("Toilet Fan", 70%, "0s");
    end;
    
    once (GetLightingState("Toilet") = OFF) then
    begin
      PulseCBusLevel("WIRED", "Lighting", "Toilet Fan", 100%, "0s", "0:03:00", 0%);
    end;
    
    
    {If fan is on when light is turned off, let it run for a few minutes}
    
    once (GetLightingState("Ensuite") = OFF) then
    begin
      if (GetLightingState("Ensuite Fan") = ON) then
        begin
          PulseCBusLevel("WIRED", "Lighting", "Ensuite Fan", 100%, "0s", "0:03:00", 0%);
        end;
    end;
     
    Last edited by a moderator: Jan 4, 2017
    NickD, Jan 4, 2017
    #6
  7. magic8

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    1
    copied and pasted what you suggested and works perfectly
    thanks for all the replies
    All good
     
    magic8, Jan 4, 2017
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.