Skip to content

Button Exercise

Exercise: Using Momentary and Action Buttons

Button Configuration

  1. Begin by creating 5 Tags. The tag names are "Counting Down", "Momentary Visibility", "Is Pressed", "Left Pressed", and "Right Pressed". Each tag has a BOOL data type with an initial value of false.

  1. Create a new script in the Script Editor called "Count Down". Set the running type to periodic.

  1. Paste the following code into the "Count Down" script.
if(tag.read("Right Pressed") || tag.read("Left Pressed") && !tag.read("Counting Down")) {
	
	var time = 10;
	tag.write("Counting Down", true);
	tag.write("Momentary Visibility", true);

	if (time > 0) {
	    for (time = 10; time > 0; time--) {
	        thread.sleep(1)
	    }
	}

	tag.write("Counting Down", false);
	tag.write("Momentary Visibility", false);
}
  • This code will be used for the objects in the next section. If either the left or right button are pressed, a 10 second timer counts down and makes another button appear.
  1. Draw 4 objects on the screen: an ellipse, one momentary button, and two action buttons.

  1. Select the ellipse object. Change the Fill type to the below condition map:

  1. Smart bind (drag-and-drop) the "Momentary Visibility" tag onto the momentary button.

  1. Under the Visibility option of the momentary button, bind the "Is Pressed" tag to it.

  1. For the left and right action buttons, go to Action, On Press, and select Create New Commands.

  1. Add a new command and select Toggle Tag Value.
    • For the left action button, bind the "Left Pressed" tag to the command.
    • For the right action button, bind the "Right Pressed" tag to the command.

  1. Repeat steps 7 and 8 for the On Release option of both the left and right action buttons.

Button Runtime

  1. Open the Canvas Simulator and test the project.

  1. Press either the left or right action button.

  1. Press the momentary button that appears. The ellipse should now turn red.