Action if condition stays true for time

Discussion in 'Pascal Logic Code Examples' started by Darren, Aug 22, 2005.

  1. Darren

    Darren Senior Member

    Joined:
    Jul 29, 2004
    Messages:
    2,361
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    A common requirement is to perform an action if some condition stays true for a certain amount of time.

    For example, you might have a requirement where a corridor needs to be switched off after an office light goes off at the end of the day. If you had :
    Code:
    once GetLightingState("Office 1") = OFF then
    begin
      Delay(20);
      SetLightingState("Corridor", OFF);
    end;
    then 20 seconds after "Office 1" goes off, the "Corridor" light will go off too. But in the case where the Office 1 light went off then back on again within 20 seconds, the Corridor light would still go off, which in this case is not desirable.

    This can be implemented correctly by using a delay then another test of the condition. For example :

    Code:
    once GetLightingState("Office 1") = OFF then
    begin
      Delay(20);
      if GetLightingState("Office 1") = OFF then
        SetLightingState("Corridor", OFF);
    end;
    This code works correctly, but because it uses a delay function, the Module will stop during this period. If this is a problem, then to keep the Module running during the delay, you can use a timer as follows :
    Code:
    once GetLightingState("Office 1") = OFF then
    begin
      TimerStart(1);
    end;
    
    once GetLightingState("Office 1") = ON then
    begin
      TimerStop(1);
    end;
    
    once TimerTime(1) = "0:00:20" then
    begin
      SetLightingState("Corridor", OFF);
      TimerStop(1);
    end;
    This second method can be added to a module quickly by doing a right click and selecting Structures | Stay True Timer.

    Eventually there will be a much simpler method of doing this.
     
    Darren, Aug 22, 2005
    #1
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.