"Once" pascal statement

Discussion in 'C-Touch/HomeGate/SchedulePlus/PICED Software' started by Neil C, Jun 28, 2007.

  1. Neil C

    Neil C

    Joined:
    Jun 27, 2007
    Messages:
    46
    Likes Received:
    0
    I don't quite get the meaning of the "once" statement which is part of the programming language within the PAC. It isn't standard Pascal, and the explanation provided by the system states that any commands which are given with the "once" statement run just once rather than all the time. Can anybody please provide any kind of explanation? Presumably the Pascal program, once uploaded, becomes resident in the PAC (or is it stored somewhere else?), and the whole program must be continually looping, otherwise it would never see the change of state of whatever is contained within the "once" statement, and thus wouldn't work as expected. If this is correct, the commands thus declared within the "once" statement are therefore running continually, not just once. If anybody could throw some light on this, I'd be very grateful.
     
    Neil C, Jun 28, 2007
    #1
  2. Neil C

    Josh

    Joined:
    Aug 25, 2004
    Messages:
    241
    Likes Received:
    0
    Location:
    Pretoria, South Africa
    Here is my attempt

    Code:
    
    SetLightingState("[COLOR="Red"]Light A[/COLOR]",ON)
    once ((GetLightingState("[COLOR="Blue"]Light B[/COLOR]") = ON) AND (GetLightingState("[COLOR="blue"]Light C[/COLOR]") = ON)) then
    begin
    SetLightingState("Light D",ON)
    end
    if(GetlightingState("[COLOR="Lime"]Light E[/COLOR]") = ON)
    begin
    SetLightingState("Light F",ON)
    end;
    
    
    In the above example let assume the logic is running on PAC and when it is turned on, the Initialize turns "Light B" and "Light C" OFF.

    In every loop "Light A" will be turned on. "Light D" will only be turned once Light B and C turns on.

    1. if between the loops, a light A is turned off using a keypad, the light will turn on again whent the PAC loops through the code.

    2. If Light B and Light C are ON, Light D will turn ON, if Light D is turned off by a keypad as well at the end of the loop or before the begining of the next loop, it will not turn on again until either lights(B and/or C) turn off and on again.

    3. if light E is ON, light F will turn on, if light F is turned off by a keypad as well (like 2), it will turn on again as long as E is ON.

    Basically ONCE means the code within the ONCE begin ..... end will only be executed the initial time the condition turns true, and it does not matter how long the condition stays true, the code within the ONCE will not be executed until the condition goes false first and then true again. You must also not that the condition needs to turn false while the PAC is online before the first true condition will be executed.

    It is like when someone says "Now listen carefully, as I will only say this once", same principle.

    Hope that helps.
     
    Josh, Jun 28, 2007
    #2
  3. Neil C

    PSC

    Joined:
    Aug 3, 2004
    Messages:
    626
    Likes Received:
    0
    Location:
    Brisbane, Australia
    PSC, Jun 28, 2007
    #3
  4. Neil C

    Richo

    Joined:
    Jul 26, 2004
    Messages:
    1,257
    Likes Received:
    0
    Location:
    Adelaide
    Yeah, not so hot on the name myself. I prefer to think of it as a transition trigger. That is when the condition defined in the once declaration transitions from false to true then the code in the block gets executed. Next pass around the code doesn't get executed because there is no transition, the condition is already true. So for the block to execute a second time the condition must first become false and then transition to true again.
     
    Richo, Jun 29, 2007
    #4
  5. Neil C

    ashleigh Moderator

    Joined:
    Aug 4, 2004
    Messages:
    2,397
    Likes Received:
    26
    Location:
    Adelaide, South Australia
    Once as in "once the moon comes up we'll have a party".

    Not once as in "once upon a time".

    It's a subtle point, too subtle. Too late to change it, but something like "WhenConditionBecomesTrue" would be a longer but more accurate way of making the keyword indicate what it actually does.
     
    ashleigh, Jun 29, 2007
    #5
  6. Neil C

    Phil.H

    Joined:
    Jul 29, 2004
    Messages:
    466
    Likes Received:
    0
    Location:
    Sydney
    That's only half the story guys...

    You have focused on the positive transition in your once descriptions. Don't forget negative transition or negative edge is just as relevant to all considerations eg
    Code:
    once GetLightingState("LightsZone1")= OFF then
    begin
       {do something}
    end;
    The PAC loops it's code approx 5 times a second so a traditional "if" statement would result in "doing something" 5 times a second (not good in the C-Bus world :) when obviously we only what it to happen "once" on the transition positive or negative (true or false). It was a very clever man who made "once" availabe in the Clipsal logic platform ;) ;)
     
    Phil.H, Jun 30, 2007
    #6
  7. Neil C

    Josh

    Joined:
    Aug 25, 2004
    Messages:
    241
    Likes Received:
    0
    Location:
    Pretoria, South Africa
    I agree, I use Once a lot ("more than once":) ) in my projects and I think it is a nifty feature and saves a lot of coding, imagine having to use ?if? and cater for the unnecessary repeats along the way.
     
    Josh, Jun 30, 2007
    #7
  8. Neil C

    Richo

    Joined:
    Jul 26, 2004
    Messages:
    1,257
    Likes Received:
    0
    Location:
    Adelaide
    The once statement only has one transition and that is from the condition being false to being true and that is what I described. Your example of checking for a light turning off is still a check for the condition (light being off) transitioning from being false to true. This is exactly what was described.

    Are you suggesting we need a trailingonce() method?

    Just putting the negated condition in a normal once should achieve the desired result, exactly as you described. What was erroneous with the description?

    Code:
    once GetLightingState("LightsZone1")= ON then
    begin
       {do something when the light comes on}
    end;
    
    once GetLightingState("LightsZone1")= OFF then
    begin
       {do something when the light goes off}
    end;
    
     
    Richo, Jul 1, 2007
    #8
  9. Neil C

    Phil.H

    Joined:
    Jul 29, 2004
    Messages:
    466
    Likes Received:
    0
    Location:
    Sydney
    The context of this thread was a description of once v's if. I have previously and successfully described this operation via a description of positive / negative or leading / trailing edge transitions. Departing from this, you are correct, from a "code" perspective it is all a statement of false to true.

    I spose it depends what direction you are looking from :rolleyes:
     
    Phil.H, Jul 1, 2007
    #9
  10. Neil C

    Richo

    Joined:
    Jul 26, 2004
    Messages:
    1,257
    Likes Received:
    0
    Location:
    Adelaide
    Arr .... OK. I have a personality flaw where I see everything as code. Doesn't work so well with my wife and 2 year old either. Although seems to work remarkably well with the dog.
     
    Richo, Jul 2, 2007
    #10
  11. Neil C

    Darpa

    Joined:
    Apr 30, 2006
    Messages:
    426
    Likes Received:
    0
    Location:
    Australia
    Gee Richo, you wouldn't happen to have worked as the Visual FX guy for a few rather popular movies now would you? :p
     
    Darpa, Jul 2, 2007
    #11
  12. Neil C

    Phil.H

    Joined:
    Jul 29, 2004
    Messages:
    466
    Likes Received:
    0
    Location:
    Sydney

    You must have a smart dog :D

    Come to think of it my dog sits around all day, gets fed regularly, allowed to stay inside when it is cold, rainy, stormy, noisy, or any other reason. When I tell her to do something she looks at me with that "yeh right - whatever you reckon" look. I work my back side off to pay for it all and clean up the back yard after the mut. How smart must I be, NOT !
     
    Phil.H, Jul 2, 2007
    #12
  13. Neil C

    [IL]NewGen

    Joined:
    Aug 3, 2004
    Messages:
    121
    Likes Received:
    0
    Location:
    Melb, Australia
    ONCE an event occurs do something once.
    IF an event occurs do something repeatively until that event changes. (Looping)

    Well that's my attempt to explain it.

    so to sum up, open IF statements are Terribad when placed in open modules that don't get disabled at the end coz they constaintly loop and will flood your processes. So you must code it wisely.
     
    [IL]NewGen, Sep 5, 2007
    #13
  14. Neil C

    nigeltodd

    Joined:
    Sep 27, 2005
    Messages:
    32
    Likes Received:
    0
    Location:
    Melbourne
    Josh Said:

    SetLightingState("Light A",ON)
    once ((GetLightingState("Light B") = ON) AND (GetLightingState("Light C") = ON)) then
    begin
    SetLightingState("Light D",ON)
    end
    if(GetlightingState("Light E") = ON)
    begin
    SetLightingState("Light F",ON)
    end;


    Josh, your explanation is good, even I can follow it. However in point 2 you state that

    "If Light B and Light C are ON, Light D will turn ON, if Light D is turned off by a keypad as well at the end of the loop or before the begining of the next loop, it will not turn on again until either lights(B and/or C) turn off and on again."

    Should that read "both lights(B and C) turn off and on again.", as you have an 'AND' between the tests in the once line?

    Regards,
    Nigel Todd
     
    nigeltodd, Sep 26, 2007
    #14
  15. Neil C

    Lucky555

    Joined:
    Aug 13, 2007
    Messages:
    229
    Likes Received:
    0
    You are right Nigel.

    If you need to get around this put the 2 GA's (or more) into a scene and test the scene.
     
    Lucky555, Sep 26, 2007
    #15
  16. Neil C

    Josh

    Joined:
    Aug 25, 2004
    Messages:
    241
    Likes Received:
    0
    Location:
    Pretoria, South Africa
    No.

    If light B or C turns off, the condition will be false (B & C), remember

    B............... C............(B and C)
    false..........false..........false
    false..........true.......... false
    true ..........false..........false
    true ..........true.......... true

    So in short, B and C do not need to be both off in this case to create a false condition. They would however need to be both on to create the true condition.

    Hope that helps clears the condition.:)
     
    Last edited by a moderator: Oct 2, 2007
    Josh, Oct 2, 2007
    #16
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.