Lessons and the learning game

Build a race timer

Two lanes, four light gates, a start tree and an LCD: a race timer good to the millisecond on an Uno, finished as a pinewood derby track.

What you build

A timer for a two-lane track. Both cars are staged in their start beams, the starter presses START, the tree counts down, and on green the cars go. The timer catches every jump start, times each lane to the thousandth of a second, prints a drag strip time slip and, in derby mode, runs four cars over four heats and crowns a champion.

How it works

Blocked is HIGH. Each gate shines infrared across a slot at a phototransistor. Light gets through and S reads LOW; a car cuts the beam and the board's pull-up takes S HIGH. The sketch watches for the moment each gate changes.

A clock that never waits. At green the sketch saves micros(), and a lane's time is the moment its finish beam is first cut, minus that. The loop has no delay() anywhere, so every gate is looked at thousands of times a second and a beam is seen within microseconds. micros() counts in steps of 4 microseconds, far finer than the millisecond it prints, and the unsigned subtraction stays right when the counter wraps.

A drag strip time slip. Every clean lane gets three numbers:

Number From To What it measures
Reaction Green Leaving the start beam The driver
ET Leaving the start beam The finish beam The car
Total Green The finish beam What wins the race

A car that leaves before green gets a red light and cannot win. Because the race is won on total, a quicker start can beat a quicker car: a holeshot.

Fair rules, chosen first. Two times less than a millisecond apart are a dead heat. A car not in after 6 seconds is a DNF. In derby mode every car runs once in each lane and its two times are added, because one lane is always a little faster and swapping cancels it out. A false start in a derby re-runs the heat.

The course

Build a race timer gets there in six lessons, all on the same wiring with a new sketch each time:

  1. Light gates: blocked reads HIGH, edges, and a car's speed from its time in the beam.
  2. Start signal: staging, three yellows and a green, and reaction time.
  3. Lane times: two lanes to the millisecond with micros(), and a DNF.
  4. Places on the display: first, second, the margin and a dead heat, laid out on a 20x4 LCD.
  5. False starts: red lights, time slips and a win on a holeshot.
  6. Project: a derby track: four cars, four heats, lanes swapped, a champion, then the real track.

Every step is checked against the running circuit, apart from the last, where you make the derby yours and share it.