Steer back onto the line
Arduino C++The starter reads the line but drives both wheels at 150 whatever it sees. Make it steer: error = position - 3500, then left = 150 + error / 20 and right = 150 - error / 20. With the line under sensor 2 the left wheel must slow and the right speed up; under sensor 7 the other way round; centred, both at 150.
Checks
- Not run yetRuns for 4.5 s
- Not run yetReads the line with readLineBlack()
- Not run yetSTBY (D9) is HIGH: the driver is awake
- Not run yetCentred: the left wheel (D5) at about 150/255
- Not run yetCentred: the right wheel (D6) at about 150/255
- Not run yetLine under sensor 2: the left wheel slows
- Not run yetLine under sensor 2: the right wheel speeds up
- Not run yetLine under sensor 7: the left wheel speeds up
- Not run yetLine under sensor 7: the right wheel slows
- Not run yetCentred again: back to 150
Hints
- readLineBlack() returns an unsigned number; cast it before subtracting: int error = (int)pos - 3500;
- error / 20 is the same as error x 0.05 without floating point: that is Kp.
- motors() already clamps each wheel to 0-255 with constrain().