Example
3.
A Two-Step
Thermostat Control
The
previous two examples use a single fan.
In this example, we use the temperature to trigger two fans in a staged
manner. The second fan is assigned
to output “fan2.” Following the
approach of Example 2, we use two set points, “setpoint1” and “setpoint2.”
title "example 3a. two-step
thermostat"
input iT1;
output oFan1, oFan2;
parameter pSetPoint1, pSetPoint2;
if(iT1< pSetPoint1)
{
oFan1=0;
oFan2=0;
}
else
{
if(iT1< pSetPoint2)
{
oFan1=1;
oFan2=0;
}
else
{
oFan1=1;
oFan2=1;
}
}
This
is an example of a nested conditional statement. The else-clause of the first conditional statement is
another conditional statement with its own condition, action and else-clause. Observe the use of the curly brackets
to delineate each compound statement , be it an action or an else-clause.
It is
possible to implement the same logic without nested conditional statement. Consider the following.
title "example 3b. two-step
thermostat"
input t1;
output fan1, fan2;
parameter setpoint1, setpoint2;
if(t1<setpoint1)
{
fan1=0;
fan2=0;
}
if( (t1>=setpoint1) &&
(t1<setpoint2) )
{
fan1=1;
fan2=0;
}
if(t1>=setpoint2)
{
fan1=1;
fan2=1;
}
The burden
of nested conditional statements is removed, but only with the added expense of
a more complicated conditional statement.
The condition above is made up of two sub conditions, namely
t1>=setpoint1, and t1<setpoint2.
These two conditions are AND-ed using the Boolean operator
“&&.” Refer to the section
“Script Syntax” for more information about Boolean
operators.
Recall
the argument in Example 1 about efficiency. The current program may also be streamlined. Consider the following.
title "example 3c. two-step
thermostat"
input t1;
output fan1, fan2;
parameter setpoint1, setpoint2;
fan1=(t1>=setpoint1);
fan2=(t1>=setpoint2);
Not only
does this version render nested conditional statements unnecessary, but also
steers clear of complicated conditions.
Of course, it is not always possible to simplify the instructions, as it
was the case here. In such cases,
the earlier examples may provide some guidelines to implementing programs for
complex applications.
Also
see : Programming, Program
Examples, Script Syntax
© Rigel Corporation iNetGrow 2003-2006. All rights reserved.