VEXCode V5 Pro Toggle Control

Hi all,
So basically I have been trying to write code that allows the driver to toggle a motor on and off with the same button (press the button, motor starts running, press button again, motor stops) and I was wondering if any of you guys programmed such a thing, and if so, what the code would look like.

Thanks y’all!

I’d do something like this:

button1Ready = true;
amount = 0;
if (Button1.pressing()){
if(button1Ready){
button1Ready = false;
if(amount==0){
amount = 100;
}
else{
amount = 0;
}
}
else{
button1Ready = true;
}
motor.spin(amount);

I’ve seen other teams use separate threads especially for more complicated purposes but I think this should be ok.

First, we must identify and have a fire-once trigger:

  //Create buttonPressed variable to be used
  bool buttonPressed = false;

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

    //Check if button A is pressed and buttonPressed is false
    if (buttonA && !buttonPressed){

      // We then set buttonPressed to true
      // This makes it that this scope (code in curly brackets)
      // Is ran once and only once when you press the button
      buttonPressed = true; 

      //ADD CODE HERE
      Brain.Screen.print("Triggered");

    }
    else if (!buttonA){ //If and only if the A button isn't pressed, we set buttonPressed to false
      buttonPressed = false;
    }


  }

Note: Assume this while true is the while true for your driver control
Run this code inside your operator control to first gain an understanding of how this works and functions (Note: ! means we flip the boolean from true to false, or false to true). Now, let’s work on having a two-option toggle:

  bool toggleEnabled = false; // <ADDED>

  //Create buttonPressed variable to be used
  bool buttonPressed = false;

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

    //////////////////////////////////////////////////////////
    // Toggle Logic
    //////////////////////////////////////////////////////////

    //Check if button A is pressed and buttonPressed is false
    if (buttonA && !buttonPressed){

      // We then set buttonPressed to true
      // This makes it that this scope (code in curly brackets)
      // Is ran once and only once when you press the button
      buttonPressed = true; 

      Brain.Screen.print("Triggered");
      toggleEnabled = !toggleEnabled; // <ADDED>

    }
    else if (!buttonA){ //If and only if the A button isn't pressed, we set buttonPressed to false
      buttonPressed = false;
    }

    //////////////////////////////////////////////////////////
    // Code For toggle Enabled or Disabled
    //////////////////////////////////////////////////////////

    // <ADDED> below
    //First time you press the button
    if(toggleEnabled){
      // Do another thing
    }
    else{
      // Do initial thing
    }

Hopefully this was detailed enough to be understood :slightly_smiling_face:

We can then compress what we have to the following:

  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
    }
4 Likes

Thanks a lot. And this would go under int main() ?

If you aren’t competing, yes.
If you are competing, it should go into user control from the competition template, found in example programs.

2 Likes