How to do automatic action in the teleop?

Hello :slight_smile:
I’m trying to implement that when I’m press a button on the joystick the robot starting an automatic actions (in the teleop periodic)…
for example when pressing a button 3 it will called a custom method:


if(stick.getRawButton(3)){
     startAutoMoving();
}

But I’m still want the all the other statements and the other action still will work - which means the I cant use here the delay() method…

Any ideas?

Sure. Your autonomous action should be implemented using a state machine. The state machine should have a START state and a DONE state. In your teleop periodic, you check for a button and then start the state machine. You also add a statement to execute the state machine in your teleop periodic code. Something like this (this is pseudocode so it is not syntactically correct):


private int autoActionState = 0;  // 0 is the DONE state.

private void runAutoAction()
{
    switch (autoActionState)
    {
        case 1:
            // do step 1
            break;

        case 2:
            // do step 2
            break;
        ....
        ....
        default:
        case 0:
            // We are done, don't do anything.
            break;
    }
}

public void teleOp_Periodic()
{
    if (button 1 is pressed)
    {
        // Start the state machine.
        autoActionState = 1;
    }

    // doing other things.
    ....
    ....

    runAutoAction();
}