Button Toggling in PROS

I made a button toggle for pneumatics in VEXcode, and translated it to PROS as I’m migrating to PROS. In theory, all of the methods line up. It works as intended on VEXcode. Yet instead of being a toggle on PROS, it’s like a normal button, where the piston extends while you’re holding the button and retracts when you let go. Any help would be appreciated. I compared documentation, pressing() & get_digital seem to do the same thing, so that’s why I’m confused. Thanks!

// To clarify the intended / expected outcome, what it does on VEXcode is I press ButtonB once and it toggles the pneumatics on, then press again to toggle them off. (So I’ m trying to replicate it on PROS, I tried multiple methods. That’s why I brought it here.)

VEXcode:

    bool toggle = false; 
    bool latch = false;

    if (toggle){
          solenoid = true; // turns clamp solenoid on
        } 
        else {
          solenoid = false; // turns clamp solenoid off
        }

        if (controller1.ButtonB.pressing()) {
          if(!latch){ // if latch false, toggle one time and set latch true
            toggle = !toggle;
            latch = true;
          }
        } 
          else {
          latch = false; //once button is released then release the latch too
          }
PROS:

	bool b_button = master.get_digital(DIGITAL_B);
	bool toggle = false;
	bool latch = false;

	if (toggle){
      piston.set_value(true); // turns clamp solenoid on
    }
    else {
      piston.set_value(false); // turns clamp solenoid off
    }

    if (b_button) {
      if(!latch){ // if latch is false, flip toggle one time and set latch to true
        toggle = !toggle;
        latch = true;
      }
    }
      else {
      latch = false; //once button is released then release the latch too
      }
2 Likes

Not sure whats going on with the “latch” but you could do this:

static bool toggle { false };
if (master.get_digital_new_press(DIGITAL_B)) {
    if (!toggle) {
        piston.set_value(true);
        toggle = !toggle;
    }
    else {
        piston.set_value(false);
        toggle = !toggle;
    }
}

Maybe it could be done more simply like this:

static bool toggle { false };    //This static variable will keep state between loops or function calls
if(master.get_digital_new_press(DIGITAL_B)) {
    piston.set(!toggle);    //When false go to true and in reverse
    toggle = !toggle    //Flip the toggle to match piston state
}

I didn’t test this code, but it should give you a general idea.

Highly recommend using okapilib with pros. It was possible to reprogram our entire bot in only a day (which took weeks on raw pros). Plus it has a lot of shiny features.

2 Likes

definitely going to use okapi for odometry once I get this done, I looked at the docs and didn’t see any other methods for buttons in okapi, which is why I didn’t bother with it for now. thanks, I will test this right away

1 Like

You can find some button functions here: okapi::ButtonBase class | OkapiLib A PROS library for programming VEX robots
The documentation is sort of difficult to use I think.

Okapilib will definitely force you to become more familiar with OOP if you are not already.

3 Likes

that code unfortunately didn’t work yields the same results as mine. i’m familiar with OOP. i dont see a difference in okapi’s methods versus PROS but i will try an okapi method just to be sure.

1 Like

It’s been awhile since I’ve read or worked through code so I’m probably way off - but in VexCode it is 'ButtonB.pressing '; are you sure that the translated into Pros code is also ‘pressing’ vs ‘pressed’?

1 Like

not 100% sure, but pressed is not what im looking for. PROS has get_digital and get_digital_new_press. i tested both and they dont work. its either that they’re not equivalent to pressing(), or something is wrong with my code. testing okapilib rn.

1 Like

ButtonBase and by extension ControllerButton’s isPressed() works!! thanks for the suggestion to look into it sooner. i guess it’s just that the pros methods are not equivalent.

1 Like

I did a search for Vex Pros toggle button and got a few hits - these 2 both seem to have a program very similar to the Pros one that you have above but with a few minor tweaks. I see you got a solution (GREAT!), but you might run a few tests with those tweaks just to see if you can determine how the methods link together better. If either of the ones below worked, they are so close to being what you had… :slight_smile:

2 Likes

thanks, when I have time later I’ll browse through and see what I did wrong if I did

1 Like

Question: are you using the pneumatics on an ADI expander or on the built in 3 wire ports?

1 Like

I’m using this https://store.robotmesh.com/vex-robotics/pneumatics/vex-pneumatic-solenoid-driver-cable-2-pack (theyre provided in the pneumatics kits as: “(2) Solenoid Driver - Cable w/Driver, Connects one VEX EDR I/O Port to one Solenoid”. so yes, 3 wire port

1 Like