Versions Compared

Key

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

...

...

...

...

...

...

...

...

...

...

...

...

...

...

Anchor
top
top
Exercise: Checking Trends History with Trend Object

Table of Contents
minLevel1
maxLevel4
include
outlinefalse
indent
excludeExercise:
typelist
class
printabletrue

Trend Configuration

  1. Create

...

  1. 3 INT (INT16)

...

  1. 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.

Image Added

2. Create a Lamp on the page. To create a Switch/Lamp,

...

go to Insert > Switch/Lamp or

...

left-click the Switch/Lamp icon in the Toolbar. Then click-and-drag on the

...

screen to place the lamp. Once the Switch/Lamp is created, drag the ON tag to the Lamp. This will bind Value to the Tag ON.

...

Image Added
  1. Click on Tools > Script Editor. Click the + icon to create a new script, then copy and paste the following code in the new script:

Code Block
languagejs
var invert = false; // Decides if the value should increase or decrease
thread.msleep(1000); // 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(1000); // waits for Count tag to update
  	if (tag.read("Count") == tag.read("Min") || tag.read("Count") == tag.read("Max")) { // Check if Count tag has reached max or min of Trend
    	invert = !invert; // inverses the invert var
  	}
}

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

Image Added

Image Added

Image Added

  1. Create the Trend by going to Insert > Trend or by simply clicking Trend on the Hotbar. In the Basic Properties of the Trend,

...

  1. drag the Max tag to Max Value and the Min tag to Min Value to

...

  1. bind them to the properties.

...

Image Added
  1. Click and hold the Count tag and drag it over 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.

...

Image Added

Trend Runtime

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

Image Added
  1. Click the Lamp to Toggle it On.

Image Added
  1. After letting it run for a while, change the Max tag to 10 and the Min tag to -10. Make sure the Count is not outside the range before changing. If Count goes out of bounds, edit the Max or Min depending on if its being added or subtracted so that it will be caught within bounds.

Image Added

Image Added