How to Program autonomous functions into a button

How would you program autonomous functions into buttons for driver control? For example, I grab a cone and I want to press a button to stack the cone by putting the chainbar back and opening the claw. I tried making a function with the automation in it and then having an if statement inside of a task. It goes like this:

task automation()
{
if(vexRT[Btn8UXmtr2])
{
motor[chainbar1] = 125;
motor[chainbar2] = 125;
wait1Msec(1000);

     motor[claw] = 100;
 motor[chainbar1] = -125;
 motor[chainbar2] = -125;
 wait1Msec(1000);
     }

}

However, when I hold down the button, the chainbar goes back and that is all. When I press it, the chainbar twitches up a little and then stops. How do I make it so that it executes the entire thing? I’m using RobotC btw.

What I would do is set it up as a function called by a button:

void automation() {
motor[chainbar1] = 125;
motor[chainbar2] = 125;
wait1Msec(1000);

motor[claw] = 100;
motor[chainbar1] = -125;
motor[chainbar2] = -125;
wait1Msec(1000);

motor[chainbar1] = 0;
motor[chainbar2] = 0;
}

task usercontrol() {
while(true) {
if(vexRT[Btn8UXmrt2] == 1) automation();
}
}

The main downside to this is the fact that you can’t run it and drive at the same time. On the flipside, this code will have the best performance. If you’re willing to multi-task, then try something more along the lines of this:

boolean automationRunning = false;

task automation() {
while(true) {
motor[chainbar1] = 125;
motor[chainbar2] = 125;
wait1Msec(1000);

  motor[claw] = 100;
  motor[chainbar1] = -125;
  motor[chainbar2] = -125;
  wait1Msec(1000);

  motor[chainbar1] = 0;
  motor[chainbar2] = 0;

  automationRunning = false;

}
}

task usercontrol() {
while(true) {
if(vexRT[Btn8UXmrt2] == 1 && !automationRunning) {
startTask(automation);
automationRunning = true;
else if(vexRT[Btn8UXmrt2] == 0) {
stopTask(automation);
automationRunning = false;
}
}
}

Hope this helps!

Quick note, I have not tested that code up above at all. If it does not work, let me know and I’ll see what I can do to help you solve your problem!

You have two primary options: Either you start the task from your main drive task on the button press, or you start a dedicated button handling task at the start of the driving period, which would observe the button on its own.
Your code above is akin to the second approach, but it’s missing a loop.
As written, it would only check the button once, then finish. So I think the only modificastion you need is an infinite loop around your code:


task automation() {
    while(true) {
        if(vexRT[Btn8UXmtr2]) {
            motor[chainbar1] = 125;
            motor[chainbar2] = 125;
            wait1Msec(1000);

            motor[claw] = 100;
            motor[chainbar1] = -125;
            motor[chainbar2] = -125;
            wait1Msec(1000);
        }
        wait1Msec(15);
    }
}

PS: Don’t forget to turn your motors off…

Thanks so much for the help, but unfortunately my team decided to halt this little project for now, but I did do the while(true) thing and it still didnt quite work.

In case you decide to revisit the project: I think your original problem was that you were setting the motor power in two places. I’m guessing you have some code in your main task that allows the driver to manually control the lift by pressing buttons and that sets the motor power to 0 when the driver is not pressing any buttons. When you start the automation task, you set the motor power to 125, but then the driver control code kicks in and sets the motor power back to 0.
That’s why just pressing the button would cause the chainbar to twitch (the motor power is briefly set to 125 and then back to 0 immediately) and why holding the button would cause the chainbar to just go back (constantly restarting the automation task and constantly setting the motor power to 125).

I would second what Lennon Headlee recommends for the solution - the simplest method is to just change the task into a function, but there are also a couple of ways to get multi-tasking to work (using a boolean to control when the automation code directs the lift and when the driver control code directs the lift).