EV3 RoboticsComponent tutorialsThe Timer

Control · Level 2 · 4 min

The Timer

Measure how long something has taken.

ComponentControl4 min

The Timer

A wait pauses for a length of time. The timer is different: it runs in the background and can be read at any moment, so the robot can know how long something has taken while it is still happening.

Blocks reference

BlockWhat it does
(timer)
Reports the seconds since the timer was last reset.
reset timer
Sets it back to zero, so the next reading counts from here.

The timeout — a safety net

The most valuable use of a timer is escaping a wait that might never end. A robot told to drive until it sees a wall will drive for ever if the wall is not there. Combined with a timer, it can give up:

Repeat until the wall is close or five seconds have passed. That one change turns a program that can hang into one that always finishes. Both robots below are looking for a wall that is not there.

no way out

repeat until distance < 15
  start moving straight: 0

with a timeout

reset timer
repeat until distance < 15 or timer > 5
  start moving straight: 0
write GAVE UP at line 1
1.2stimer212cm · distance
Both robots are told to drive until something is within 15 cm. The room ahead is empty.
timer 1.2 s

The sensor is not faulty and the program is not wrong. There is simply no wall, and only one of these two programs has a way of noticing that.

The left-hand robot is not broken, and neither is its sensor. Its condition is simply one that will never come true, so the program sits on that block for ever — with nothing on the Brick to say so. The right-hand program asks the same question with an escape route bolted on, and finishes every time.

Why it matters

Real systems time themselves out constantly — a lift that cannot close its doors eventually gives up and beeps rather than trying for ever. A robot with no timeout simply stops responding, and there is nothing on screen to say why.

More control tutorials