Skip to content

Switch/Lamp Exercise

Exercise: Activating a Number Counter

Switch/Lamp Configuration

  1. Create two tags. One tag will be named "Status" and will be a BOOL tag. The other tag will be named "Counter" and will be a INT16 tag. When done creating a tag, click OK to finalize the changes.

  1. Create a Switch/Lamp object and a Numeric Text Object. Click Insert > Switch/Lamp or the Insert Switch/Lamp icon in the toolbar. Click and drag on the page to bring up the Switch/Lamp object. Click Insert > Numeric Text or the Insert Numeric Text icon in the toolbar. Click and drag on the page to bring up the Numeric Text object.

  1. Bind the "Status" tag to the Switch/Lamp object. Click and hold the "Status" tag and drag it to the Switch/Lamp object. Release the mouse button to bind the tag to the Switch/Lamp object. Another way would be to click the Switch/Lamp object, go to the Basic Properties tab, and click the Bind button on State. Select the "Status" tag from the list and click OK.

  1. Bind the "Counter" tag to the Numeric Text object. Click and hold the "Counter" tag and drag it to the Numeric Text object. Release the mouse button to bind the tag to the Numeric Text object. Another way would be to click the Numeric Text object, go to the Basic Properties tab, and click the Bind button on Value. Select the "Counter" tag from the list and click OK.

  1. Create a script to increment the "Counter" tag. Click on Tools > Script Editor to open the Script Editor. Click New to create a new script. Name the script "Counter Enable" and click OK. The script should be a manual script. Copy and paste the following code into the script:
tag.write("Status", !tag.read("Status"));
thread.msleep(100);
while(tag.read("Status")){
	tag.write("Counter", tag.read("Counter") + 1);
	thread.msleep(300);
}

  1. On the Switch/Lamp, go to the Actions tab. Under On Press, click the Add button. Select the "Counter Enable" script from the list and click OK.

Switch/Lamp Runtime

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

  1. Click the Switch/Lamp object to toggle the "Counter" tag. The Numeric Text object should increment by one every time the Switch/Lamp object is toggled.

  1. To stop the counter, click the Switch/Lamp object again to toggle the "Counter" tag off.

  1. Change the Counter Value to any random number to change the starting number.

  1. Click the Switch/Lamp object to toggle the "Counter" tag. The Counter value should incrememnt from where it was last set.