Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Anchor
top
top
Exercise: Observing Data Change with a Trend.

Table of Contents
minLevel1
maxLevel4
include
outlinefalse
indent
excludeExercise: Observing Data Change with a Trend.
typelist
class
printabletrue

Trend Configuration

  1. Create 3 INT (INT16) tags named Count, Max, and Min. Count will have an Initial Value of 0, Max will have an Initial Value of 20. Min will have an Initial Value of 0. Lastly, create one BOOL tag named ON. The Initial Value should be false.

2. Create a Lamp lamp on the page. To create a Switch/Lamp object, go to Insert > Switch/Lamp or left-click the Insert Switch/Lamp icon in the Toolbar. Then, click - and - drag on the screen to place the lamp. Once the Switch/Lamp object is created, drag the ON tag to the Lamp. This will bind the Value field to the Tag ON.

  1. Click on Tools > Script Editor. Click the + icon to create a new script; on the popup window, click OK. Copy and paste the following code into the new script:

Code Block
languagejs
var invert = false; // Decides if the value should increase or decrease
thread.msleep(100); // Waits for Lamp tag to update
while (tag.read("ON")) { // While Lamp tag is On (1)
  	tag.write("Count", invert ? tag.read("Count") - 1 : tag.read("Count") + 1); // If Invert is false, Add 1 to Count tag, if invert is true, Subtract 1 from Count tag
  	thread.msleep(500); // Waits for Count tag to update
    if (tag.read("Count") < tag.read("Min")) {tag.write("Count", tag.read("Min"))} // Assigns Min to Count if Count is less than Min
  	else if (tag.read("Count") > tag.read("Max")) {tag.write("Count", tag.read("Max"))} // Assigns Max to Count if Count is greater than Max
  	thread.msleep(500); // Waits for Count tag to update
  	if (tag.read("Count") == tag.read("Min") || tag.read("Count") == tag.read("Max")) {invert = !invert} // Inverses the invert var if Count has reached Max or Min.
}

Once pasted in, close the Script Editor by clicking OK. Select the Lamp, lamp and go to the Actions tab. If Actions does not display, go to View > Properties > Actions to make it displayand check the box. Under On Press, click the Action Field field and Create New Command. Click on Add New Command and select Toggle Tag Value. Under ParametersIn the Tag field, select the ON tag. Then, in the top left of the Command Editor, click the + icon , and for to create a new command. Change the new command , select to Call Script and select the created script that was created.

  1. Create the Trend a trend by going to Insert > Trend or by simply clicking Trend on the Hotbarleft-click the Insert Trend icon in the Toolbar. Then, click and drag on the screen to place the trend. In the Basic Properties of the Trend tab, drag the Max tag to the Max Value field and the Min tag to the Min Value field to bind them to the properties.

  1. Click and hold drag the Count tag and drag it over the Trendto the trend. This will create a new Pen pen in the Basic Properties of the Trendtrend. Go to Pen 1, and change the Pen Width to 5.

Trend Runtime

  1. Click Tools >Launch Simulator to launch the Canvas Simulator.

  1. Click the Lamp lamp to Toggle toggle it On. When the Lamp lamp turns on, the Count tag will start to increase update every second. Once First, it starts at the Min of 0, increasing to 20, then once it reaches the Max of 20, it will then start to decrease every second until it hits decrease back to the Min . Then starts to increase again and of 0. This will repeat the process until turned offindefinitely or until the lamp is toggled Off.

  1. After letting it run for a while, change the Max tag to 10 and the Min tag to -10. From this, if If the Count is outside the boundaries, it will immediately jump to the closest boundary and function normally. The Trend will now start recording data in the new Max and Min and will stay in that range until the values are changed againcontinue the function as described in the step above.