beautypg.com

Saved variables – Contemporary Control Systems BASview User Manual

Page 66

background image

TD110500-0MC

66

Saved Variables

Normal Python variables lose their value every time the program completes a run and are undefined at
the beginning of the next run. It is sometimes useful to be able to remember a value so it may be used on
the next execution of a program. This is supported by the setVariable and getVariable functions.

setVariable("bias", 12.5)

#store the value of bias

myBias=getVariable("bias")

#get the value and store in myBias

Note the required quotes around the desired variable name ("bias"). These variables are local to this
program only. The same variable names can be used in many programs. Each program may have up to
10 saved variables.

Calling getVariable with a variable name that has not been defined will always return zero.

If a variable is no longer needed, use removeVariable("myVariable") to remove it from the saved variables list.

Here is an example that extends the override switch example above:

Do not use the following example before reading this entire section. It is potentially dangerous if not

set up properly.

if getValue(@mySwitch)>0:

#Is the switch being pressed?

isOverridden = getVariable("isOV")

#get stored value

if isOverridden:

#if it is already overridden

release(@myLights)

#release myLights

releaseSchedule(@mySched)

#release schedule

setVariable("isOV",0)

#set isOV to 0

else:

#if it is not overridden

setValue(@myLights,1,3600)

#override myLights

overrideSchedule(@mySched,1,3600)

#override schedule

setVariable("isOV",1)

#set isOV to 1

The above example uses

getVariable

and

setVariable

to remember if the override is currently active. If it is

not active and the switch is pressed it overrides the points. If the override is already active, it releases the
points from program control. The points will then fall back to the value the rest of the system determines
they should have (from a schedule, etc.).

The effect of this to the user is that if he needs to stay late, he can press a switch to keep things running
for 1 more hour. If he then wants to leave 20 minutes later, he can hit the switch again to release the
points to their normally scheduled values.

The above example still has a potential problem. There is no check to see how long it has been since the
last change of override state. This means that if a user holds the switch down for 10 seconds, it could
override the points on then off several times before the user releases the switch.

This can be avoided by setting the program to execute every 10 seconds. But what if the user holds the
button for 20 seconds or someone later changes the execution interval to 1 second?