This is an arduino sketch for a line following robot based on infrared LEDs and an H-bridge.
The code uses a PID controller to control the wheels of the vehicle so it steers into a dark line. To improve the likelihood of the vehicle sliding off the line, the code decreases the speed of the robot when it enters a sharp turn.
The speed of the motors is controlled via pulse frequency modulation. This allows the motors to be moved under the influence of cogging torque and stiction as the pulses act like a jackhammer. The end result is motion similar to a stepper motor as the motor moves in discrete jumps of variable length. This greatly improves low speed control of the motors at the cost of jerkyness. At higher throttle settings, the motors switch over to PWM to smoothen the motion.
The code requires a circuit like the one shown in this schematic:
Below are images of the robot this code was written for:
Here's a video of the same robot in operation: