Newbie: Using variables


#1

Give a description of the problem:

I would like to use a variable to store a device state at a given moment for use later on:

Define Variable boolean {Output State} = False

IF Output changes to on
Then:
DO:
Set variable {Output state} = True

What is the expected behavior?
I Would like the Variable to NOT change back to “False” if the Output is turned back off.

What is happening/not happening?
If the Output is turned off the variable state changes to false again

Should I use a number value instead of the Boolean True/False?
With my “no Programming” knowledge I find it counter intuitive that the State would change back to false as there is no code that describes (If Output changes to OFF - Set variable = false)

Hmmm…

Thank you for a great forum, I’m learning every day :wink:


#2

Anytime we set a variable up top in the “define” section, it will force that variable each and every time the piston executes.

If you want to be strategic as to when it updates, leave it (not set) up top, and use the command “Set variable” somewhere inside the “execute” portion of your code. This way, the variable will only update when you want it to.


Also, I would recommend not using spaces in the names of your variables


#3

What happens is that the full piston, including any defines, is run each time a trigger event occurs. This is great if you want the piston to start each time with a definite value, but not so great if you want the piston to maintain an assigned value between runs.
The solution is not to set it in the define at all. With a boolean, the first time the piston runs the value is indeterminate. However, my experience is that it is never “true”. So, even if you put a test up before the variable is set and test for not true, it should work correctly.


#4

If you need to set a value before the piston runs you can scroll down to “variables” and click the edit icon next to the variable.

I use this to set a bool to an initial value when I’ve first created a piston.