Don't brake in an empty room
Arduino C++This robot's bumper sketch lights the brake (D13) when something is closer than 20 cm. It works against a wall, but in an open room it brakes too: when nothing echoes, pulseIn() returns 0 and 0 cm is "closer than 20". Brake only when there really was an echo under 20 cm. The wall is 80 cm off, 15 cm at 1.5 s, gone at 3 s and 8 cm away at 4.5 s.
Checks
- Not run yetRuns
- Not run yetWall at 80 cm: no brake
- Not run yetWall at 15 cm: brake
- Not run yetEmpty room: no brake
- Not run yetWall at 8 cm: brake
Hints
- ping() returns 0 when nothing came back. 0 is not a distance.
- Both must be true: there was an echo, and it was close.
- bool brake = cm > 0 && cm < 20;