CodeRigor

GUIDED LESSON / 1 OF 6

Define a useful distance display

Keep measurement units and missing echoes explicit.

What moves through this step
  1. 01Echo duration
  2. 02Millimetres
  3. 03State policy
  4. 04LED display

Build a tabletop display

Move a flat object in front of an ultrasonic sensor. Red means close, yellow means caution and green means clear within this lab's configured range. A missing or out-of-range echo produces unknown with all three LEDs off and a serial explanation.

This is a desk experiment. Its readings and timing do not establish a vehicle-parking aid, collision-avoidance system or reliable control system.

The sensor returns an echo duration in microseconds. Sound travels out and back; using a nominal 343 m/s gives echo_us * 343 / 2000 millimetres. Temperature, surfaces, angle and timing affect a physical reading, so measure against a ruler later.

Begin with the C++ policy

Download and unzip the project. On a computer with a C++ compiler run:

Bash
c++ -std=c++17 -Wall -Wextra -Werror test_beacon.cpp -o test_beacon
./test_beacon

The executable checks distance bands, invalid input, echo conversion and scheduling arithmetic. It does not flash a board or measure a real echo.

Try it

Define outputs for 0, 79, 100, 250 and 4001 mm before reading the implementation. Then ask what should happen around a boundary if successive measurements differ by a few millimetres.