#include // include servo library // Controlling servo with Sharp IR Sensor // Create servo object Servo myServo; // Variables // Variable to hold sensor's value int servoValue = 0; // Variables to hold servo's angles int currentAngle = 0; int lastAngle = 0; // Pin variables int sensorPin = 5; int servoPin = 10; // Timer variables long timer = 0; int offset = 10; // Setup void setup() { Serial.begin(9600); pinMode(servoPin,OUTPUT); myServo.attach(servoPin); myServo.write(servoValue); timer = millis(); } // Das loop... void loop() { // Assumption: Sensor voltage numbers range // from 0 (nothing in front) to ~ 680 (~ 3 inches) servoValue = analogRead(sensorPin)/3; Serial.println(servoValue); if(timer + offset < millis()) { if(lastAngle > servoValue) { currentAngle = currentAngle - 1; myServo.write(currentAngle); lastAngle = currentAngle; timer = millis(); } if(lastAngle < servoValue) { currentAngle = currentAngle + 1; myServo.write(currentAngle); lastAngle = currentAngle; timer = millis(); } } }