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 ContentsminLevel1maxLevel4includeoutlinefalseindentexclude

.printabletrue
typelistclass

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 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 and go to the Actions tab. If Actions does not display, go to View > Properties > Actions and check the box. Under On Press, click the Action field and Create New Command. Click on Add New Command and select Toggle Tag Value. In the Tag field, select the ON tag. Then, in the top left of the Command Editor, click the + icon to create a new command. Change the new command to Call Script and select the created script.

  1. Create a trend by going to Insert > Trend or left-click the Insert Trend icon in the Toolbar. Then, click and drag on the screen to place the trend. In the Basic Properties 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 drag the Count tag to the trend. This will create a new pen in the Basic Properties of the trend. 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 to toggle it On. When the lamp turns on, the Count tag will start to update every second. First, it starts at the Min of 0, increasing to 20, then once it reaches the Max of 20, it will decrease back to the Min of 0. This will repeat indefinitely 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. If the Count is outside the boundaries, it will jump to the closest boundary and continue the function as described in the step above.