Two-Button Toggle

Hey guys, been looking around for a toggle program and found one that I didn’t think was easy for the average programmer to glance at and understand, so I attempted to make an easier to understand version that does the same thing. This code is for using two buttons to control one toggle, one to open, one to close. (If you want to have one button change Btn6U to Btn6D or vice-versa)


void toggle()
{
if(vexRT[Btn6U] == 1 & counter < 1 )
{

counter = counter + 1;

}

if(vexRT[Btn6D] == 1 & counter == 1)
{

counter = counter - 1;

}
}

You can then use the counter value as a toggle.

-944_Nick (Still need to activate my account)

If you’re wanting a two-button toggle, then I think you’re overcomplicating things a bit. Just make the buttons turn on a boolean.


bool toggled = false;
void toggle(){
if(vexRT[Btn6U]){
toggled = true;
}

if(vexRT[Btn6D]){
toggled = false;
}
}

If you’re wanting a one-button toggle, then you need another boolean to keep track of the last value of the controller (assuming you’re constantly calling toggle() in your control loop).


bool toggled = false;
bool prevPressed = false;
void toggle(){
if(vexRT[Btn6U] && !prevPressed){
toggled = !toggled;
}
prevPressed = vexRT[Btn6U];
}