Embedded C Robots: Barcode Reader and Maze Solver
First-year mechatronics (MSE 110), built as a team on a LEGO EV3 programmed in C with RobotC: four robots in one term, each a small embedded system of sensors, motors with encoders, a control loop and a state machine. The two that went furthest are here: a barcode reader that decodes Code 39 letters from a single light sensor, and a robot that explores a grid maze, then drives back along the shortest route.
- Course
- MSE 110 Mechatronics Design I
- Year
- 2017
- Platform
- LEGO EV3, RobotC (C)
- Analysis
- MATLAB
Four robots, one term
- Line follower. Bang-bang steering on the reflected-light reading at the edge of a line, plus an ultrasonic check that stops the robot within 8 cm of an obstacle and runs an arm motor before carrying on.
- Sumo. A scan-and-charge state machine: sweep about ±65° with encoder-counted pivots until the ultrasonic sensor sees the opponent within 25 cm, lower a blade until a touch switch confirms it is down, charge, and turn back in when the colour sensor sees the white ring edge.
- Barcode reader. A light sensor on a motorised carriage sweeps a printed Code 39 letter; the robot logs light against encoder position and the decoder recovers the letter.
- Maze solver. Explore a 4 × 6 grid of tape lines to a target cell, record the route, prune it, and return by the shortest path.
Barcode reader: measuring position, not time
A Code 39 character is nine elements, five bars and four spaces, three of them wide (about three times a narrow one). Reading it means measuring widths, so the first design decision was to index every sample by the carriage motor's encoder rather than by time: the motor's speed then doesn't matter. The firmware streams (encoder count, reflected light) pairs into the EV3's datalog every 3 ms while the motor sweeps the sensor across the card, stops when a touch switch at the end of travel closes, then rewinds the carriage to zero.
The decoder then treats the log as a signal-processing problem:
- Smooth. A 25-sample moving average removes the sensor's quantisation steps and flicker at the bar edges.
- Differentiate. A five-point centred finite difference, f′ᵢ ∝ −fᵢ₊₂ + 8fᵢ₊₁ − 8fᵢ₋₁ + fᵢ₋₂, turns each bar edge into a peak. It is fourth-order accurate, so the peaks are sharp without amplifying noise as much as a simple difference.
- Find edges. Smooth again, take the absolute value so rising and falling edges look alike, and keep peaks above a prominence threshold: ten edges for nine elements.
- Classify widths. Divide each edge-to-edge spacing by the smallest one and round down: ratios of 1 are narrow, 2 – 4 are wide. That makes the decoder independent of print size and sweep speed.
- Look up. Match the nine-element narrow/wide pattern against a 26-row Code 39 table.
% edges -> element widths -> narrow (1) / wide (0) -> letter
[~, loc] = findpeaks(edgeStrength, 'MinPeakProminence', 0.35);
w = diff(loc); % edge-to-edge spacing in samples
w = floor(w / min(w)); % 1 = narrow, 2..4 = wide
w(w >= 2) = 0; % table stores wide as 0
for k = 1:26
if isequal(w, code39(k,:))
disp(char('A' + k - 1));
end
end
Running that pipeline on one of our recorded sweeps gives the spacing ratios 1, 1, 3, 1, 3, 3, 1, 1, 1, which is the letter F, the card that sweep was taken from.
Maze solver: control loops inside a state machine
The maze is a grid of black tape lines on white, and the robot carries its light sensor on a third motor so it can swing the sensor left, centre and right. Each step of the exploration is the same sequence of states:
- Track the line between intersections. A PD loop on the reflected-light error steers the two drive motors around a base speed: e = light − setpoint, turn = Kp·e + Kd·(e − e_prev), left = base − turn, right = base + turn. The distance is fixed by encoder counts (about 1,780 ticks per cell), so the robot always stops on the next intersection.
- Scan the intersection. Swing the sensor to the right, centre and left positions (encoder targets on the sensor motor) and threshold each reading against the white level.
- Choose a direction. Right if a line goes right (right-hand rule), otherwise straight, otherwise left, otherwise a 180° turn out of a dead end. Turns are encoder-counted pivots.
- Log the move. Update the heading (0 = N … 3 = W) and the row and column, and append the heading to a move array.
Driving straight between scans used a second, simpler loop that I like as an example of how much a few lines of feedback buy: one motor is the master, and the slave's power is corrected from the encoder difference every 100 ms, so the robot doesn't curve when the two motors aren't matched.
// keep both wheels at the same distance: P-correct the slave motor
while (abs(totalTicks) < ticks)
{
motor[right] = masterPower;
motor[left] = slavePower;
error = nMotorEncoder[right] - nMotorEncoder[left];
slavePower += error / kp; // integer P correction
resetEncoders();
wait1Msec(100); // 10 Hz correction loop
totalTicks += nMotorEncoder[right];
}
Once the robot reached the target cell, it returned without exploring again. Any move followed immediately by its opposite (into a dead end and straight back out) cancels, the remaining headings are reversed in order and each is flipped by 180°, and the robot replays that list.
Results
- 4robots in one term, each a sensor, actuator and control-loop system in C
- 9 → 1Code 39 elements decoded to one letter from a single light sensor
- ~333 Hzbarcode sampling, every sample indexed by encoder position
- 4 × 6maze grid explored with PD line tracking and solved back by path pruning
How this page was made
These were team projects and we wrote the code together; the excerpts are short, lightly tidied pieces of our code. The barcode figure is one of our recorded sweeps, run through the decoder's method and re-plotted for this page. The maze figure is an illustration of the pruning rule, not a logged run.