Skip to main content
Skip table of contents

7.1 - Doing Things Based on Input (Applies to T-Series and UD-Series)

Often one will want to monitor an input and then perform an action depending on what that input reading is. For example, we could be monitoring the temperature of a greenhouse and want to turn a heater on when it gets cold, and off when it gets hot. This is a basic thermostat. Assuming we had two channels, "Temperature", an analog input which has been converted to degrees C, and "HeaterPower" which is a digital output, to create a simple thermostat that turns on the heater at 15C and off at 22C we'd do this:

1) Click on the + next to CHANNELS: in the Workspace to expand this item and show all the channels.

2) Click on the Temperature channel.

3) Select the Event tab. Here we can enter script that executes every time a new reading occurs on the Temperature channel. Since this script can delay the readings, we need to make sure its short and fast.

4) Enter the following script:

if ((Temperature[0] < 15) && (Temperature[1] >= 15))
HeaterPower = 1
endif
if ((Temperature[0] > 22) && (Temperature[1] <= 22))
HeaterPower = 0
endif

5) Click Apply. The thermostat will start working immediately, though it won't actually change the state of the heater until the temperature passes through one of the thresholds.

This example shows a couple things that are a least important to thermostat applications:

  • Hysteresis: we could just have the heater go on if the temperature is below 15 and off it is above, but this usually does not work well. Your analog inputs will likely have at least a little noise, and so when the temperature is right around 15, the readings will tend to jump above and below the 15 threshold causing the heater to rapidly turn on and off. To avoid this, we use something called hysteresis. The heater comes on at 15 degrees, but doesn't shut off until 22. Unless your noise is 7 degrees, the heater will respond much better.

Thresholding: another way to write this script would be:

if (Temperature[0] < 15)
HeaterPower = 1
endif
if (Temperature[0] > 22)
HeaterPower = 0
endif

The difference is that in this second, shorter script, the heater power digital output is set every time Temperature is read and is above or below the given threshold. This may create quite a bit of overhead on the device. Even the UE9 takes a few milliseconds to perform each action and if you are doing a lot of different things with the device, this can add up.

So, in the original script, we look at the most recent point, [0], and the next most recent point, [1], and only adjust the HeaterPower output if they are on opposite sides of the threshold, meaning the temperature has just dropped below, or risen above the threshold. Once this occurs, the HeaterPower output won't be reset until the other threshold is crossed.

Sample file: LJGuideSamples\Thermostat.ctl

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.