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 one a button that only emits a signal while it is pressed. When released, the signal turns off again.
First, make a tag called “isPressed” to indicate whether the button is pressed or not (“isPressed”). Do this by going to the Tag Editor pane, then choosing Add Tag:
...
To make this look more like a temporary push button, we’ll choose a different image for each state. Use “Push Button Static“ for state 0, and “Push Button Active” for state 1.
...
At this point, you should have the following in the Script Editor:
...
and you should have the following in the Actions pane for the button object:
...
Save the project and run the simulator. Pressing and releasing the button should change the Switch/Lamp imagestate:
...
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 for to represent the lamp. We will use bind a tag to the opacity of the “on“ image to simulate the lamp turning on and off gradually.
Here, we We will use the images “3D Lamp Off 010” and “3D Lamp On 010” from the Graphic Library:
...
Position the images so that they overlap, with the “on” image displayed on top. Resize the images as desired. You should have the following:
...
Since the opacity property expects a value between 0 and 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 (100), or partially on (1–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 = Math.min(1, amountPressed)> 100) amountPressed = 100; tag.write("amountPressed", amountPressed); thread.msleep(10); } while (tag.read("isPressed")); |
...
Set the “isPressed” tag to true.
Get the current value of the “amountPressed” tag and store it to the variable
amountPressed
.In a
while
loop, wedo
the following:Increase the value by a small amount (0.01)1.
Clamp the value so that it doesn’t go over 1100.
Write the updated value to the opacity “amountPressed“ tag.
Sleep for a small amount of time (10 milliseconds).
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 =< Math.max(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 button lamp is not in use.
Now run Save the project and run it in the simulator. While the button is pressed, the lamp should gradually get brighter. When the button is released, the lamp should gradually get dimmer.
...