One button for on and off

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

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