> For the complete documentation index, see [llms.txt](https://docs.cb21829.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cb21829.org/ftc-java-advanced-guide/finite-state-machines.md).

# Finite state machines

Finite state machines are one of the most useful patterns in FTC.

Instead of blocking through a whole routine, you define states and move between them a little each loop.

### The pattern with an enum

```java
import com.qualcomm.robotcore.util.ElapsedTime;

public enum LiftState {
    IDLE,
    EXTENDING,
    DUMPING,
    RETRACTING
}

LiftState liftState = LiftState.IDLE;
ElapsedTime liftTimer = new ElapsedTime();

switch (liftState) {
    case IDLE:
        if (gamepad1.a) {
            lift.setTargetPosition(LIFT_HIGH);
            lift.setMode(DcMotor.RunMode.RUN_TO_POSITION);
            lift.setPower(0.8);
            liftState = LiftState.EXTENDING;
        }
        break;

    case EXTENDING:
        if (!lift.isBusy()) {
            dumpServo.setPosition(DUMP_POS);
            liftTimer.reset();
            liftState = LiftState.DUMPING;
        }
        break;

    case DUMPING:
        if (liftTimer.seconds() > 1.0) {
            dumpServo.setPosition(STOW_POS);
            lift.setTargetPosition(0);
            liftState = LiftState.RETRACTING;
        }
        break;

    case RETRACTING:
        if (!lift.isBusy()) {
            liftState = LiftState.IDLE;
        }
        break;
}
```

### Why this beats blocking code

Each state does a quick check and returns. The rest of the loop keeps running, so drivetrain, other mechanisms, and telemetry keep updating too.

### Scaling up

* give each subsystem its own FSM
* use FSMs to drive autonomous routes
* move to command-based structure when the state logic becomes too large


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cb21829.org/ftc-java-advanced-guide/finite-state-machines.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
