VCS pressed button

Hi all!
I am new to programming and I am wondering if in VCS you can make a motor turn on and off at the press of a button. what I mean is if you press L1, one time and release it, could you make it stay on until you hit the button again? any and all help will be greatly appreciated!

Team 373A

1 Like

I’m not exactly sure how to do it with a single button, but last year my team did that with our flywheel with two buttons as shown. Btw, our teams programmer had zero experience so I’m unsure how good it is. Hopefully this helps. image

1 Like

start with this.

http://help.vexcodingstudio.com/#cpp/namespacevex/classvex_1_1controller_1_1button/pressing

There are also numerous topics discussing how to do this.

1 Like

okay, that makes sense thank you all sooo much!

I don’t have time to write the code right now, but I would probably do something like:

if (button pressed & motor off){
Turn motor on
Wait until button is unpressed
}
Else if(button pressed & motor on) {
Turn motor off
Wait until button is unpressed
}

All in a while loop

Tara,
Would this be betrer than what the other people told me? It seems difderent.

It is different than what the others shared. What you originally asked was press a button to turn the motor on, then press it again to turn it off. What @jpearman shared was one button for on, and a different one for off. What I shared would accomplish what you originally asked.

You’d want to use something like this to accomplish the wait the code below would have the code wait until the button is unpressed. Use “false” instead of “true” for waiting until unpressed.

while (controller1.buttonL1.pressing==true){}

Okay, that makes sense, thank you!

1 Like

The code below should do what you are trying to do. Basically, it sets a variable to 0 and 1, and then sets the motor to spin or not based on whether a is 0 or 1. Sorry about the messy indenting. Good luck! :smile:

    //set an integer to allow for switching
int a;
a = 0;
while(1) {
  //these two if statements toggle the value of a
  //sleep to stop bouncing
  if(Controller1.ButtonUp.pressing() && a == 0) {
    a = 1;
    vex::task::sleep(2000);
  }
  else if(Controller1.ButtonUp.pressing() && a == 1) {
    a = 0;
    vex::task::sleep(2000);
  }
if(a == 0) {
 don't spin motor;
}
if(a == 1) {
 spin motor;
}
1 Like

Awesome! Thank you, I was just about to ask how you would do it with variables.

You can do either what I said or what @RandomKidOnAVexTeam said, except if you use his you want to switch

vex::task::sleep(2000);

to

while(controller1.buttonup.pressing){}

What does that do? The sleep function was to get rid of bouncing. It could be reduced to 700 or even 500, but I don’t understand the point of the while statement.

It pauses the code while the button is still being pressed, such that it won’t bounce, but also starts the program back up again immediately once you release the button. It doesn’t matter if you press the button for a nanosecond or a year, it will still work correctly. You don’t have to guess how long a button press lasts.

1 Like

Wouldn’t you want a wait one msec in the while statement, or is that not necessary?

Well yes, I forgot to include that. You don’t want to waste processing power.

1 Like

Thank you.
20 Characters.

bool motorRunning = false;

void buttonPressCallback()
{
    if(!motorRunning)
    {
        //startMotor
        motorRunning = ture;
    }
    if(motorRunning)
    {
        //stopMotor
        motorRunning = false;
    }
}


int main()
{
    Controller.ButtonL1.released(buttonPressCallback);

    while(true)
    {
        //your code
    }
}

when ever L1 is pressed the function buttonPressCallback is run once. It will run once when the button is pressed and then released, you can change the released to pressed so that you function runs once the moment the button is pressed (not waiting for it to be released).

More

The released/pressed take in a function to be called. Also the function will not stop any of the code in you main loop from running.
Also here is the code shortened:

bool motorRunning = false;

 int main()
{
    Controller.ButtonL1.released([](){ //this is called a lambda
        motorRunning = !motorRunning; //'!' is a not and turns true to false and vise versa
        if(motorRunning) //startMotor
        else //stop motor
    });

    while(true)
    {
        //your code
    }
}

Lambda

2 Likes

Please do not do any of these. They stop all other code in your loop from running and sleeping for 2 seconds will be the wrong amount of time (also not that important but a does not have to be an int it can be a bool which is just true or false).

Here is a solution similar to yours…

bool a = false;
bool b = ture;

int main()
{
	while(true)
	{
		if(Controller1.ButtonUp.pressing() && b) a = !a, b = false;
		if(!Controller1.ButtonUp.pressing()) b = true;

		if(a) //run motor
		else //stop motor
	}
}

This way a is whether the motor is running or not. a gets chnaged when the button is pressed and b is true. And b is true until the button gets pressed. Then it is false until the button is unpressed. This way a changes only once. Also note that this way a is changed the moment that the button pressed.

Like this it is changed when the button released:

bool a = false;
bool b = false; //false so that it doesn't trigger in the vary beginning

int main()
{
	while(true)
	{
		if(!Controller1.ButtonUp.pressing() && b) a = !a, b = false;
		if(Controller1.ButtonUp.pressing()) b = true;

		if(a) //run motor
		else //stop motor
	}
}

The change is that the not sign is now in the first if statement instead.

Also if you want the motor run command to be run only once you can do this:

if(Controller1.ButtonUp.pressing() && b)
{
    a = !a, b = false;
    if(a) //run motor
    else //stop motor
}
if(!Controller1.ButtonUp.pressing()) b = true;
2 Likes

Yes. A callback would work as well, but it’s a little harder to implement. I gave the easy solution such that it would be easy for OP to understand and implement.