Skip to content

Line Following Reflectance Sensor Simulator

Run MicroSim in Fullscreen

Specification

The full specification below is extracted from Chapter 7: Building and Programming a No-Display STEM Robot.

Type: microsim
**sim-id:** line-following-reflectance-simulator<br/>
**Library:** p5.js<br/>
**Status:** Specified

Learning objective: Students will *analyze* (Bloom L4: Analyze) how a pair of reflectance sensor readings determines a line-following robot's steering correction at each moment along a curving path.

Canvas: 700x480px, responsive -- recompute the drawn line path and robot icon scale from `windowWidth`/`windowHeight` in `windowResized()`.

Layout: a top-down arena with a dark curving line pre-drawn on a light floor (an S-curve or similar simple path). A robot icon with two small sensor indicator dots (left and right) drives along the path automatically once started, each dot lighting up green when it detects the line beneath it and gray otherwise. A live readout beneath the arena shows the current sensor state pair (e.g., "Left: ON, Right: OFF -- correcting right") matching the branches of the pseudocode in the surrounding chapter text.

Controls: a `createButton()` labeled "Start/Pause" toggling the robot's automatic movement along the path; a `createSlider()` labeled "Base Speed" (range 20-100, default 70) scaling the robot's forward progress speed; a `createButton()` labeled "Reset to Start" that returns the robot to the path's beginning; a `createSelect()` dropdown labeled "Path Shape" with options "S-Curve," "Zig-Zag," and "Loop" that swaps the pre-drawn line path.

Interaction: as the robot drives, each of the four sensor-state branches from the chapter's code (both on, left only, right only, both off) is visually distinguished by a different robot icon border color, and the matching branch in a small on-screen pseudocode panel is highlighted in sync, echoing the flowchart/pseudocode highlighting pattern from Chapter 1. Clicking anywhere on the drawn line path teleports the robot to that point along the line for quick testing of a specific curve section.

Implementation: p5.js, path stored as an array of points defining a spline curve; robot position advanced along an interpolated point on the curve each frame, with simulated sensor readings computed by checking whether each sensor dot's offset position (left/right of the robot's centerline) falls within a threshold distance of the nearest path point. Recompute path and robot scaling in `windowResized()`.