Controlling a motor direction and speed