IF

Description: Trigger to perform an action. It changes the active state in a module instance, or executes a script, or both.
Returns: Nothing
Usage: Steady State only.
Function Groups: Logic Control
Related to: IfElse | IfOne | IfThen | Cond
Format:
IF Trigger Destination;
[
  Script 
]
Parameters:  
Trigger
Required. Any logical expression. If false, nothing happens. If true, the action is performed, and any functions that may be reset are automatically reset.
Destination
Optional. Destination is not an expression, it is the text name of a state. If provided, it must be a legal name of a state in the module where this action is defined.

It is not possible to change to a state in another module. Each module must have one (and only one) active state, unless the module has no states defined.
Comments: This is the only way to change the active state in a module. An action may have a Destination, a Script, or both, or neither (although it does not make sense to have neither).

Script is a list of functions and statements, ending in semicolons, contained by square brackets. The Script is optional and may be omitted. There is no limit to the number of statements in Script (other than RAM). If the script is omitted, the square brackets must also be omitted. A script's statements are executed in order, from top to bottom. While a script is executing, no other statements, functions, or scripts may execute until this script is complete.

Examples:

highLevel = 0;
...
FillUp 
[ { Begin state FillUp }
  If level > 36 heaterOn { Wait for tank to fill }; 
] { End state FillUp }

This action is triggered if the variable level is a number greater than 36. If this action is triggered, the state FillUp is stopped, and the state HeaterOn is started.

{ An example of poorly written code }
HeaterOn 
[ { Begin state HeaterOn }
  If level > 40 { Monitor the level }; 
  [ { Begin script } 
    highLevel++; 
    highAlm = highLevel > 10 { Sound high level alarm }; 
  ] { End script } 
] { End state heaterOn }

This is an example of what to AVOID!

If level rises above 40, this action will trigger over and over again. This is called an "If 1" condition and will degrade system performance easily to 10% or 1% or less of its normal potential. All the time level > 40 is true, highLevel will increase by one at a very rapid rate. This is because there is no destination state. The action remains active, and the trigger is checked again and again.

Note also that the script has two statements. They are executed once only, in order. This means that the script will have to execute 10 times before the first statement has increased highLevel enough so that the second statement will execute.

If what is desired is to count the number of times level rises above 40, a better way to write this would be:

HeaterOn 
[ { Begin state HeaterOn }
  If Change(level > 40, 0) && level > 40
  { Monitor the level }; 
  [ { Begin script } 
    highLevel++; 
    highAlm = highLevel > 10 { Sound high level alarm }; 
  ] { End script } 
] { End of state heaterOn }

This waits for level to change while also being above 40. After this happens, the action is triggered, the script is executed, and the Change function is automatically reset to wait for another change. The Change function will not re-trigger until level drops below 40, when there will finally be a change in the condition level > 40. At that time, however, since level will be less than 40, the action won't trigger until level rises above 40. This is how to trigger on the rising edge of a logical expression (level > 40).

Refer to "Latching and Resetting Functions" for further information.