Timer

The Timer class provides non-blocking time tracking for the Echo microcontroller. Unlike the delay() function which halts all program execution, Timer allows your robot to perform multiple time-dependent tasks simultaneously without interruption.

Why Use Timer Instead of delay()?

The standard delay() function stops your entire program, which can cause problems in robotics applications:

  • delay() blocks everything: Motors, sensors, and communication all freeze

  • Timer runs in parallel: Check multiple timers while your robot continues operating

  • Better control flow: Implement timeouts, intervals, and scheduled events without stopping other operations

Important: You can still use delay() in setup() for initialization, but avoid it in loop().


Key Features

  • Non-blocking time measurement

  • Start, stop, pause, and resume functionality

  • Automatic pause at specified time

  • Millisecond and second precision

  • Multiple independent timers in one program

  • Zero impact on other program operations


OverView

The Timer class provides a flexible timing utility for measuring elapsed time with support for pause, resume, and auto-pause functionality.

Method
Description

Timer()

Creates a new timer instance in stopped state

start()

Starts the timer from zero

stop()

Stops the timer and resets accumulated time

pause()

Pauses the timer, preserving current time

reset()

Resets timer to zero and stops it

setPause(ms)

Sets automatic pause when specified milliseconds are reached

resume()

Resumes the timer from paused state

isPaused()

Returns true if timer is currently paused

get()

Returns elapsed time in milliseconds

getSeconds()

Returns elapsed time in seconds as a float


Quick Start


API Reference

Timer()

Creates a new timer instance. Multiple timers can be created for tracking different events.

Example:


start()

Starts or restarts the timer from zero. Resets any accumulated time and begins counting.

Example:


stop()

Stops the timer and resets all accumulated time to zero. Use this to completely halt and clear the timer.

Example:


pause()

Pauses the timer while preserving the accumulated time. The timer can be resumed later from where it stopped.

Example:


resume()

Resumes a paused timer, continuing from where it was paused.

Example:


reset()

Resets the timer to zero without starting it. The timer remains stopped until start() or resume() is called.

Example:


setPause(unsigned long ms)

Configures the timer to automatically pause when it reaches the specified time in milliseconds.

Parameters:

  • ms: Time in milliseconds at which to auto-pause

Example:


isPaused()

Returns whether the timer is currently paused.

Returns:

  • true: Timer is paused

  • false: Timer is running or stopped

Example:


get()

Returns the elapsed time in milliseconds.

Returns:

  • unsigned long: Elapsed time in milliseconds

Example:


getSeconds()

Returns the elapsed time in seconds as a floating-point number.

Returns:

  • float: Elapsed time in seconds

Example:


Practical Examples

Example 1: Blinking LED Without delay()

Example 2: Multiple Timed Events

Example 3: Timeout Detection


Best Practices

Comparing Times

Use >= instead of == when comparing timer values to avoid missing the exact moment:

Resetting Timers

After triggering an event based on time, remember to reset and restart:

Multiple Independent Timers

Create separate timer instances for different tasks:


Common Patterns

Interval Timing

Execute code at regular intervals:

One-Shot Timer

Execute code once after a delay:

Accumulative Timing

Track total active time with pause support:


Technical Details

Time Resolution

  • Precision: 1 millisecond (based on Arduino millis())

  • Range: Up to 49.7 days (limited by unsigned long overflow)

  • Accuracy: Dependent on system clock (typically very accurate)

Memory Usage

Each Timer instance uses minimal memory:

  • Approximately 20 bytes per timer

  • No heap allocation

  • Suitable for creating many timers

Performance

Timer operations are extremely fast:

  • get() and getSeconds(): ~1-2 microseconds

  • start(), stop(), pause(), resume(): ~0.5 microseconds

  • Negligible impact on loop performance


Troubleshooting

Timer Doesn't Start

Problem: Timer shows zero time.

Solution: Ensure start() is called before checking time:

Time Seems Frozen

Problem: Timer value doesn't change.

Solution: Check if timer is paused:

Events Fire Multiple Times

Problem: Timed event executes repeatedly.

Solution: Reset timer after triggering:

Auto-Pause Not Working

Problem: setPause() doesn't stop timer.

Solution: Check the timer with isPaused() in your loop:

Last updated