Your code looks fine and the wiring is done, but the robot stubbornly misses the white line? Does it treat the white line as black floor and drive over it, or treat black floor as the line and turn wrongly / stop?
Do not panic; the code may be fine. Often the real issue is missing eye adjustment — that is, calibration.
How are our tracks?
Many online tutorials describe white floor + black line. On our contest and workshop tracks it is the opposite: black floor with a white line. The idea is the same; only which color you treat as the line changes.
What is wrong? Lighting tricks
Line sensors (especially reflective IR sensors) are sensitive to ambient light. Workshop light is not the same as competition-hall light. Values measured at home can shift in another hall.
So do not hard-code a magic number and hope:
if (sensor < 500) {
// I assume I am on the line
}
500 may be right in your workshop and wrong on the contest track.
What does the sensor read? (our measurement)
- White (line): near 0 (strong reflection)
- Black (floor): near 1023 (weak reflection)
So for us: line = low value, floor = high value.
The tip: Teach the robot the room
Every time you enter a new environment (new hall, new track, different lighting), measure floor and line again.
1) Measure the white line
Put the sensors fully on the white line. Note the Serial value. Example: 50 (near 0).
2) Measure the black floor
Place the robot on black floor (off the line). Note the value. Example: 1000 (near 1023).
3) Find the middle (threshold)
threshold = (white + black) / 2
// Example: (50 + 1000) / 2 = 525
That is your real threshold for that moment and that room. In code:
if (sensor < threshold) {
// on the white line (low value)
} else {
// on black floor (high value)
}
Workshop note: Use
Serial.println(sensor)while calibrating; turn Serial off before the track (testMode = 0).
When should you recalibrate?
- When you move to a different hall or track
- When lighting changes (sun, spotlights, dim rooms)
- When sensor height or angle changes
- When floor or tape color changes
Summary
Our track: black floor, white line. Measure white and black, average them for the threshold, and look for the line at low values. Correct threshold = a robot that can see.
Checklist: White line measured (near 0)? Black floor measured (near 1023)? Threshold from the average? Recalibrated in the new hall?