Press Button to run a auton section in driver?

Hello, My builder is getting on me about this but how could i code in driver control when a button is pressed, our arms and intakes lift the tray up without me doing it manually? yes its kind of dumb but our tray is a pain to deploy in driver and our auton deploy works 100%. Is there any way to do that?
Thanks,
Butters

1 Like

A lot of Vex teams refer to it as “macros”. But the general idea is to take your auton deploy and encase it in a function. Then on the first button press, call that function.

6 Likes

ah. But how will i do it?

i have the else statement stop the motors.
Would i just do else if then if button pressed- run that section of the auton by Copying and pasting that section?
Im sorry if i am being stupid. I am not the best at coding and we have states in 2 weeks

I created an sample competition template below. In this example I created two functions because my team has two different paths that they want the robot follow depending on which side they are on. Then, in the autonomous() function I simply uncomment the program I want the robot to run.

Since I have each auton program in a function then I can call that function at anytime in my code. In the usercontrol I added code so that if a button is pressed then it runs that auton program.

//Sample comp code without comments.
#include "vex.h"
using namespace vex;
competition Competition;
controller Controller1;

void pre_auton(void) {
  vexcodeInit();
}

void autonBlue(void){
  //My team's autonomous code for the blue side.
}

void autonRed(void){
//My team's autonomous code for the red side.
}

void autonomous(void) {
  //We're going to be on the blue side for the next match.
  autonBlue();
  //autonRed();;
}

void usercontrol(void) {

  while (1) {
    //Include my standard controls to operate my robot

    //If I press ButtonUp then it will run autonBlue()
    if(Controller1.ButtonUp.pressing()){
        autonBlue();
    }
    wait(20, msec); // Sleep the task for a short amount of time to
  }
}

int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);
  pre_auton();

  while (true) {
    wait(100, msec);
  }
}
3 Likes

I know how to do it on blocks but you start off by doing when driver control>if(controller of choice) (button of choice) is pressed? then> ( commands you want so here you would put motor(x) spin 100 and like that) else> ( here you put to stop the motors.) I am yet to do this on my bot since i can only work on it at home but I can demonstrate my macro on which I use to move my intakes simultaneously.

so can i just use a section of code from my auton to driver? Like i have it stated in auton can i just say if button pressed. spin the motors to a certain rotation?

literally like a regular button press then copy and paste. I would make sure it isnt required to hold down the button throughout.
So if button pressed then copy paste the auton deploy code and it will be fine

2 Likes

Best coding practice is to “not repeat your code” but yes you could just copy your code.

Better solution is to put your code in a function and then call that function from both places. That way if you change your program you only have to do it in one place.

7 Likes

Okay thank you all for your solutions. I will try to learn more about coding during the off season so i dont have to ask questions as much.

it would go something along the lines of…

void flipOut() {
     insert flip out code here
 }

void opControl() {
    while(true) {
    Controller.ButtonUp.pressed(flipOut);
    }
}
1 Like

So i have to make a new void for the flip out code? and can i just do the controller thing in the driver control or will i need to put it in a new void

The term is “function” not “void”. The term “void” is a “type”, just like “int” or “double” are types. The “void” type just happens to not hold a value.

If you associate the function with a button “pressed” event, it will be called when you press the button. Do NOT put this registration inside a while loop, or every time thru the loop, a new association will be made and your function will not work as expected.

Unfortunately sometime it is not always as simple as this. If you have a function that lifts your arms to some predefined height and also have a button that you hold that raises them or lowers them (perhaps inside an “if button.pressing” condition), then you need to have a way for these two methods of telling the motor(s) what to do in a coordinated fashion.

6 Likes

Please do not put a call to register a pressed event inside of a loop. The example should be:

void flipOut() {
     // insert flip out code here
 }

void opControl() {
    Controller.ButtonUp.pressed(flipOut);
    while(true) {
       // Other user control here
    }
}
2 Likes

Can anyone spot the logical problem (I’m away from the programming environment, so this may not compile) in the code below (assume ArmMotor is a motor declared elsewhere):

void flipOut() {
       ArmMotor.setVelocity(50.0, percent);
       ArmMotor.spinToPosition(180, deg, true); 
}

void opControl() {
    Controller.ButtonUp.pressed(flipOut);
    while(true) {
     if(Controller.ButtonRight.pressing()) {
          ArmMotor.setVelocity(75.0, percent);
          ArmMotor.spin(fwd);
     } else if(Controller.ButtonLeft.pressing()) {
          ArmMotor.setVelocity(75.0, percent);
          ArmMotor.spin(rev);
     } else {
          ArmMotor.stop();
     }
     vex::task::sleep(50);
    }
}
2 Likes

UPDATE—
It works. thank you every one.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.