Darren
22 Aug 05, 12:23 PM
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 :
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 :
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 :
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.
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 :
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 :
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 :
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.