> For the complete documentation index, see [llms.txt](https://marrow.skeletonarmyftc.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://marrow.skeletonarmyftc.com/automations/timerex.md).

# TimerEx

package com.skeletonarmy.marrow

## Overview

`TimerEx` is an extended timer utility built on top of `ElapsedTime`. It provides convenient methods for tracking elapsed time, remaining time, and includes extra features such as pause/resume support.

`TimerEx` can be used as:

* A simple stopwatch (no duration provided).
* A countdown timer for match time.

Check out the ["Time Awareness" page](https://marrow.skeletonarmyftc.com/concepts/time-awareness.md) to learn how to use `TimerEx` for autonomous behaviors.

***

## Usage

### Setup

To use `TimerEx`, simply instantiate it with your desired settings (default unit is seconds):

```java
TimerEx stopwatch = new TimerEx();
TimerEx stopwatch = new TimerEx(TimeUnit unit);

TimerEx timer = new TimerEx(double duration);
TimerEx timer = new TimerEx(double duration, TimeUnit unit);
```

To start the timer, call `start()`:

```java
TimerEx timer = new TimerEx(30);
timer.start();
```

### Methods

The `TimerEx` class provides the following methods:

| Method                        | Description                                                     |
| ----------------------------- | --------------------------------------------------------------- |
| `start()`                     | Starts the timer (safe to call multiple times, only runs once). |
| `restart()`                   | Resets the timer.                                               |
| `pause()`                     | Pauses the timer.                                               |
| `resume()`                    | Resumes the timer.                                              |
| `getElapsed()`                | Returns the time passed since the timer was started.            |
| `getRemaining()`              | Returns the remaining time for the duration.                    |
| `isLessThan(double timeLeft)` | Returns `true` if remaining time is less than `timeLeft`.       |
| `isMoreThan(double timeLeft)` | Returns `true` if remaining time is greater than `timeLeft`.    |
| `isDone()`                    | Returns `true` if the timer reached 0.                          |
| `isOn()`                      | Returns `true` if the timer is currently running.               |

***

## Example Usage

Here’s an example autonomous that uses `TimerEx` together with SolversLib’s command system:

{% hint style="info" %}
This is by no means a functional autonomous program.
{% endhint %}

```java
@Autonomous
public class MyAuto extends CommandOpMode {
    TimerEx matchTime = new TimerEx(30); // 30 second autonomous
    DriveSubsystem driveSubsystem = new DriveSubsystem();

    @Override
    public void initialize() {
        schedule(
                // Display the remaining time
                new RunCommand(() -> telemetry.addData("Time Remaining", matchTime.getRemaining())),

                // Choose actions based on time left
                new ConditionalCommand(
                    new ScoreCommand(), // If enough time remains
                    new ParkCommand(driveSubsystem), // If almost out of time
                    () -> matchTime.isMoreThan(3)
                ),

                // Attempt a fast score if only a few seconds remain
                new ConditionalCommand(
                    new ScoreCommand(), // If enough time remains
                    new FastScoreCommand(), // If almost out of time
                    () -> matchTime.isMoreThan(3)
                ),

                // Automatically park when 5 seconds remain
                new SequentialCommandGroup(
                        new WaitUntilCommand(() -> matchTime.isLessThan(5)),
                        new ParkCommand(driveSubsystem)
                )
        );
    }

    @Override
    public void run() {
        super.run();
        telemetry.update();

        matchTime.start(); // Runs only once
    }
}
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://marrow.skeletonarmyftc.com/automations/timerex.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
