So I want to prgram a piston to be able to open and close i a single button, however I don’t know how I was wondering if anyone could help me with this if its even possible, I really don’t know.
You’ll want a global state variable. The pseudocode is:
bool toggleState = false;
// declaration of your piston
void handleButtonXPress() {
toggleState = !toggleState; // The ! operator is the logical not. This statement changes the value of toggleState each time handleButtonPress is called
if(toggleState) {
piston.open();
} else {
piston.close();
}
}
void main() {
controller.ButtonX.pressed(handleButtonXPress); // This registers your function to get called each time ButtonX is released. It is important NOT to put this inside a loop
// rest of the code
}
5 Likes
You only need one callback function. Inside the function, have an if statement that checks if the pneumatic is extended or retracted, then if it is extended, it retracts. If it is retracted, it extends. If this doesn’t make sense, I’ll whip something up to show you what I mean.
Thank you so much! I think im understanding it, I will try this in my programming.
Another way to do this that doesn’t use a global variable is to do
piston.set(!piston.value());
This gets the current state of the piston then sets it to whatever state it isn’t using !.
1 Like