Control a servo with a ir sensor

I think that the big problem is how you are waving the servo around. I think you need to get rid of the for loops, that are waving the servo around. Create one while loop, instead, that moves the servo to another position, if the sensor reads less than 600. Don't move, and don't increment the position if the sensor reports more than 600.

The amount to increment by should be changed to a negative value when the position value is at or above the upper limit for the servo, and should be changed to a positive value when the position value is at or below the lower limit for the servo.