Versions Compared

Key

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

For this exercise, we will create a button that gradually turns a lamp on when pressed, then off again when unpressed.

...

  • Create a temporary push button (basic).

  • Use the button to gradually control a lamp object (advanced).

Making a Temporary Push Button

A temporary push button is a button that only emits a signal while pressed. When released, the signal turns off again.

...

Save the project and run the simulator. Pressing and releasing the button should change the Switch/Lamp state:

...

Changing a Value Gradually

Now, create another tag that will gradually change between 0. 0 and 1.0100:

...

Instead of a single Switch/Lamp object, we will use two image objects to represent the lamp. We will bind a tag to the opacity of the “on“ image to simulate the lamp turning on and off gradually.

...

Position the images so that they overlap, with the “on” image displayed on top. Resize the images as desired. You should have the following in the Project Tree:

...

Since the opacity property expects a value between 0 and the following in the page:

...

1, but the tag value will range from 0 to 100, we will use a scaling expression.

Select the “on” image object, then bind the “amountPressed” tag click the binding icon next to its opacity property:

...

...

In the Binding Properties window, go to the Expressions tab. For the expression type, choose “Scale Tag Value”, then use the following options:

...

Press OK to close the Binding Properties window.

If you run the simulator, you can manually change the value of the “amountPressed” tag using the tag monitor. This should make the lamp look like it is off (0), on (1100), or partially on (in between1–99).

Now we can modify the button’s scripts to gradually increase and decrease the value of the “amountPressed” tag.

...

Code Block
tag.write("isPressed", true);
var amountPressed = tag.read("amountPressed");
do {
	amountPressed += 0.011;
	if (amountPressed > 1100) amountPressed = 1100;
	tag.write("amountPressed", amountPressed);
	thread.msleep(10)
} while (tag.read("isPressed"));

...

  1. Set the “isPressed” tag to true.

  2. Get the current value of the “amountPressed” tag and store it to the variable amountPressed.

  3. In a while loop, we do the following:

  4. Increase the value by a small amount (0.01).1.

  5. Clamp the value so that it doesn’t go over 1100.

  6. Write the updated value to the “amountPressed“ tag.

  7. Sleep for a small amount of time (10 milliseconds).

  8. When the “isPressed” tag gets set to false, we end the loop. This happens when the user lets go of the button, in the “On Release” script.

...

Code Block
tag.write("isPressed", false);
var amountPressed = tag.read("amountPressed");
do {
	amountPressed -= 0.011;
	if (amountPressed < 0) amountPressed = 0;
	tag.write("amountPressed", amountPressed);
	thread.msleep(10)
} while (!tag.read("isPressed") && amountPressed > 0);

Note that on line 8, we added the condition that amountPressed must be greater than 0 for the cycle to continue. This means that once the value goes back down to 0, the script ends. This way, we don’t need to leave any scripts script threads running in the background while the lamp is not in use.

...