Program Rovers to stay in the middle of a lane marked out by walls, a simplification of the lane-assist technology in cars. Use the front ultrasonic sensor and both IR sensors to determine where the walls are and drive as smoothly through the middle of the path as you can. Don’t run into the walls!
Setup
Create a curving walled passage on the floor. The walls can be anything from books to wooden blocks, as long as they’re solid and tall enough to be detected by the IR and ultrasonic sensors. Tight passages and sharp corners greatly increase the difficulty. You can also add coloured tape as traffic markers that the Rover must stop at or turn around at.
Our Approach
The core of this problem is: if the wall on the left is closer than the wall on the right, you need to turn right. This can be done with IF statements, get that working first before looking at this solution. Once you’re confident, check out the PID example to make it smoother.
Stage 1: Read the Sensors
Using clear variable names makes code easier to read and follow. We use variables to calculate the difference between the distances read by the two IR distance sensors.
Stage 2: Calculate Motor Speeds
The Scale Number math block converts a number from one range to another. We used −35 to 35 because that suited the width of our passage, about the maximum difference possible between the left and right distance sensor readings. We converted that to −7 to 7 so the maximum difference between the motor speeds is 14 cm/s.
The left and right motor speeds are then calculated by adding or subtracting this value from a base forward speed, so the Rover always moves forward.

Example code, IR-based lane centring
Once your Rover is navigating the lane smoothly, try narrowing the passage or adding sharper turns. You can also experiment with a full PID controller to get even smoother movement through the course.


