One button for on and off

Hi. I am trying to make button L1 for example close my pneumatic claw, and then I click button L1 again to open it. Is this possible, and if so does anyone have any examples? I have searched around, but could not find anything. Thank you!

How To Code a Toggle
This post talks about how to code a toggle in VEXCode:

From that, the toggle code is as such:

  bool toggleEnabled = false; // two-choice toggle, so we use bool
  bool buttonPressed = false; // IGNORE, logic variable

  while (true){
    
    bool buttonA = Controller1.ButtonA.pressing();

    ////////////////////////////////////////////////////////////////////
    // Toggle Logic
    if (buttonA && !buttonPressed){
      buttonPressed = true; 
      toggleEnabled = !toggleEnabled;
    }
    else if (!buttonA) buttonPressed = false;

    ////////////////////////////////////////////////////////////////////
    // Code For toggle Enabled or Disabled
    if(toggleEnabled){
      // Do another thing
    }
    else{
      // Do initial thing
    }

You can find more information about toggles by using the search bar:
Capture
How to setup pneumatics in motors and sensors setup
https://kb.vex.com/hc/en-us/articles/4404204989076-Controlling-Pneumatics-Using-Buttons-on-Your-Controller
How to Code Pneumatics

piston.set( true );

and

piston.set( false);

Putting it all together

  bool toggleEnabled = false; // two-choice toggle, so we use bool
  bool buttonPressed = false; // IGNORE, logic variable

  while (true){
    
    bool buttonA = Controller1.ButtonA.pressing();

    ////////////////////////////////////////////////////////////////////
    // Toggle Logic
    if (buttonA && !buttonPressed){
      buttonPressed = true; 
      toggleEnabled = !toggleEnabled;
    }
    else if (!buttonA) buttonPressed = false;

    ////////////////////////////////////////////////////////////////////
    // Code For toggle Enabled or Disabled
    if(toggleEnabled){
      piston.set(true);
    }
    else{
      piston.set(false);
    }
6 Likes

Thank you so much. You are so helpful!

1 Like

Anytime!
In the future when you ask a question and someone answers it, make sure to mark their response as a “Solution” to let others know that an answer has been posted (plus it is a good motivator for people who spend time trying to respond to continue helping the forum).

1 Like

Ok. I added it to yours. Would a velocity toggle work in the same way? Like the same button Pressed part but the last part to
If (ButtonPressed) {
Wheels.setVelocity(100) } else {
Wheels.aetVelocity(200) };

I know that is not the right format. I just wrote it straight to the forum. Would that work for wheel velocity slowdown toggle?

Yes, that should work perfectly fine based on what I see in the code and context :slight_smile:

Ok. Thank you. Sorry for the confusing questions

1 Like