V5 text if button pressed twice

so currently me and my team are trying to program our lift mechanisms to where if you press the button once it will move to a certain degree but if pressed twice it will move the arm to a certain degree the 2 degrees are different , we seem to not be able to find any information on this

You could add a integer that changes whenever the button is pressed. For example,

int f
 if(button pressed){
f++;
}
if(f=0){
}
if(f=1){
f==0;
}

my code is most likely inaccurate as I have moved to pros

would
pressing()==true
make sense
or is that wrong

that is supper wrong its just telling if pressing is true

Its pseudocode, not real code, if you were to implement this it would be

//include motor setup and controller.
int f=0;
while(true){
if(Controller1.ButtonL1.pressing()){
f++;
}
if(f=1){
//action here
}
else if(f=2){
//second action here
f==0;//reset f counter to 0
}
}

here is slightly updated with that I actually put into vexcode instead of just typing it on forums

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Controller1          controller                    
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;
int f=0;
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  if(Controller1.ButtonA.pressing()){
    f++;
  }
  if(f==1){
    //action
  }
  else if(f==2){
    //2nd action
    f=0;
  }
}
2 Likes

Would this work for turning on/off the intake with the a single button, so that the driver does not have to hold a button down while driving?

thanks, ill try it out

You might consider putting this into the Controller1.ButtonS.pressed(callback) callback function. The reason being that you’ll likely have this within a while(true) loop that has a small sleep statement at the end (maybe 20 milliseconds or something similar).

Consider how this will behave in a loop. The first time thru with ButtonA being pressed, f will go from 0 to 1. On the loop’s next iteration (20 milliseconds later), it is unlikely that the human will have released the button yet, so now, f will go from 1 to 2 (which will actually then set it back to 0. If the action is setting the direction of the intake (suppose), you will likely observe the intake “not working”. You may see it engage and disengage briefly, or you may think that it never actually turns on, in spite of pressing the button.

2 Likes

you really need everything inside a while loop that constantly checks for button press, you should also wait for button release before checking again otherwise the state variable (f in code posted earlier) will be changed too quickly. The code @crabrave posted implements a state machine with two states, here’s another simple example that can easily be extended to as many states as you need.

code
/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       james                                                     */
/*    Created:      Fri Nov 29 2019                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/
#include "vex.h"

using namespace vex;

// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain       Brain;
vex::controller  Controller;

// define your global instances of motors and other devices here

int main() {
    int  state = 0;

    while(1) {
        // if button pressed
        if( Controller.ButtonA.pressing() ) {
          // wait for button release
          while( Controller.ButtonA.pressing() )
            this_thread::sleep_for( 20 );

          // so something based on the state variable
          switch( state ) {
            case 0:
              Brain.Screen.clearScreen( red );
              break;
            case 1:
              Brain.Screen.clearScreen( green );
              break;
            case 2:
              Brain.Screen.clearScreen( blue );
              break;
            
            default:
              // this is an error and should never be reached
              break;
          }

          // increment state variable
          // and handle overflow          
          if( state++ >= 2 )
            state = 0;
        }
    
        // Allow other tasks to run
        this_thread::sleep_for(10);
    }
}
3 Likes

It actually sounds to me like you want it like a computer mouse, with a single click, double click or maybe even a triple click? Some of my IQ students did this with their autonomous, so that pushing once would run one program, twice a different one…