---
title: "Trend Exercise"
canonical: "https://manual.cimon.com/space/AUR/4492263425/Trend%20Exercise"
format: markdown
---
# > Macro (anchor)

<span style="color: #403294">**Exercise: Observing Data Change with a Trend**</span>

- [Exercise: Observing Data Change with a Trend](https://cimon.atlassian.net/wiki/spaces/AUR/pages/4492263425/Trend+Exercise#[inlineExtension]Exercise:-Observing-Data-Change-with-a-Trend)
  - [Trend Configuration](https://cimon.atlassian.net/wiki/spaces/AUR/pages/4492263425/Trend+Exercise#Trend-Configuration)
  - [Trend Runtime](https://cimon.atlassian.net/wiki/spaces/AUR/pages/4492263425/Trend+Exercise#Trend-Runtime)

## <span style="color: #4c9aff">**Trend Configuration**</span>

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.

![image](media://ebe101d9-ef2a-4d2f-8789-eb74274da7f8)

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](https://cimon.atlassian.net/wiki/spaces/AUR/pages/4488265730). 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.

![image](media://fa16f6b4-86b4-40dc-974d-5678d133cf57)

3. 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:

```javascript
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.

4. Create a trend by going to **Insert **>** Trend **or left-click the **Insert Trend** icon in the [Toolbar](https://cimon.atlassian.net/wiki/spaces/AUR/pages/4488265730). Then, click and drag on the screen to place the trend. In the [Basic Properties](https://cimon.atlassian.net/wiki/spaces/AUR/pages/4490821641/Trends#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.

![image](media://4bd90f4a-1718-4bfd-b55f-d9686d00c684)

5. Click and drag the Count tag to the trend. This will create a new pen in the [Basic Properties](https://cimon.atlassian.net/wiki/spaces/AUR/pages/4490821641/Trends#Basic-Properties) of the trend. Go to **Pen 1**, and change the **Pen Width** to 5.

![image](media://4b2670d8-ca42-4fa7-aee8-ca71abbb116d)

## <span style="color: #4c9aff">**Trend Runtime**</span>

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

![image](media://4fea5baa-7d13-4a4c-b9a8-19bed5c0c47d)

2. Click the lamp to toggle it On. When the lamp turns on, the Count tag will 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.

![image](media://a52aa119-cad8-4990-92da-3817fccdb912)

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

##### <span style="color: #4c9aff">[Back to top of Trend Exercise](#top)</span><span style="color: #4c9aff">.</span>