Exercise: Observing Data Change with a Trend.
Trend Configuration
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, 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.
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:
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 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.
Create the Trend by going to Insert > Trend or by simply clicking Trend on the Hotbar. In the Basic Properties of the Trend, drag the Max tag to Max Value and the Min tag to Min Value to bind them to the properties.
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.
Trend Runtime
Click Tools >Launch Simulator to launch the Canvas Simulator.
Click the Lamp to Toggle it On. When the Lamp turns on, the Count tag will start to increase every second. Once it reaches the Max of 20, it will then start to decrease every second until it hits the Min. Then starts to increase again and will repeat the process until turned off.
After letting it run for a while, change the Max tag to 10 and the Min tag to -10. From this, 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 again.
Add Comment