# TONR — On-Delay Timer

Source: https://rungs.dev/docs/structured-text/instructions/tonr

> TONR is the Structured Text on-delay timer function block in rungs.dev. It uses FBD_TIMER and mirrors the behavior of the ladder TON instruction.

Structured Text on-delay timer function block. Uses `FBD_TIMER`; the ST equivalent of ladder [`TON`](https://rungs.dev/docs/ladder-logic/instructions/ton).

## Operands [#operands]

| Name    | Type        |
| ------- | ----------- |
| `timer` | `FBD_TIMER` |

## `FBD_TIMER` Tag Members [#fbd_timer-tag-members]

| Member           | Type   | Meaning                                                                        |
| ---------------- | ------ | ------------------------------------------------------------------------------ |
| `.EnableIn`      | `BOOL` | Forced to `1` each scan by the instruction call — setting it `0` has no effect |
| `.TimerEnable`   | `BOOL` | `1` runs the timer and advances `ACC`; `0` stops timing                        |
| `.PRE`           | `DINT` | Preset target time in milliseconds                                             |
| `.Reset`         | `BOOL` | `1` clears `EN`, `TT`, `DN` and sets `ACC` to `0`                              |
| `.EnableOut`     | `BOOL` | `1` while the instruction is executing normally                                |
| `.ACC`           | `DINT` | Accumulated time in milliseconds                                               |
| `.EN`            | `BOOL` | `1` while `TimerEnable` is `1` and `Reset` is `0`                              |
| `.TT`            | `BOOL` | `1` while the timer is actively counting toward `PRE`                          |
| `.DN`            | `BOOL` | Done status — each timer (`TONR`, `TOFR`, `RTOR`) decides when this turns on   |
| `.Status`        | `DINT` | Packed status word — combines `InstructFault` and `PresetInv` bits             |
| `.InstructFault` | `BOOL` | `1` when the instruction hit an execution error                                |
| `.PresetInv`     | `BOOL` | `1` when `PRE` is outside its valid range                                      |

## How It Works [#how-it-works]

`TONR` is the Structured Text function-block equivalent of the ladder [`TON`](https://rungs.dev/docs/ladder-logic/instructions/ton) instruction. It operates on an `FBD_TIMER` tag and exposes the same on-delay behaviour via named members.

When called with a true `TimerEnable`, the timer accumulates toward `.PRE`. `.DN` becomes `1` when `.ACC` reaches `.PRE`.

## Example [#example]

```iecst
MyTimer.TimerEnable := Start_Button;
MyTimer.PRE := 5000;
TONR(MyTimer);

IF MyTimer.DN THEN
    Alarm := 1;
END_IF;
```

> **IDEA**
>
> **Practice**
>
> Try `TONR` in the [Long Press exercise](https://rungs.dev/exercises/long-press) — fire only when a button is held for two seconds.

## Common Mistakes [#common-mistakes]

- Trying to pause with `.EnableIn := 0`. The instruction call forces `.EnableIn` back to `1` every scan, so the assignment has no effect. To stop timing, drop `.TimerEnable` to `0` — that resets `.ACC` to `0`.
- Restarting the timer on its own done bit (`MyTimer.Reset := MyTimer.DN;`) to make it repeat. The call after a reset only restarts the timer and adds no time, so every cycle runs two scans longer than `.PRE`. That is fine for a flashing light, but a total counted from those cycles falls further behind every cycle. To keep time, let one timer run and work from its `.ACC`.
- Assigning `.ACC` from logic to reset it (`MyTimer.ACC := 0`). Structured Text recomputes `.ACC` every scan, so a runtime write is **ignored** (the initial `.ACC` default is still honored). Clear it with `.Reset := 1`. The same write _does_ take effect on the ladder [`TON`](https://rungs.dev/docs/ladder-logic/instructions/ton) — so don't rely on it either way; it isn't portable.

## Related [#related]

- [TON](https://rungs.dev/docs/ladder-logic/instructions/ton) — the ladder-logic equivalent
- [TOFR](https://rungs.dev/docs/structured-text/instructions/tofr), [RTOR](https://rungs.dev/docs/structured-text/instructions/rtor) — other ST timer function blocks