// Controlling a servo position using a potentiometer (variable resistor) // by Michal Rinott // Modified by Khazar, 11/2008 #include // Include the Servo Library. // Libraries are kept in the "hardware/libraries" subdirector of the // Arduino application directory. Servo myservo; // Create servo object to control a servo int potpin = 0; // Analog pin used to connect the potentiometer int val; // Variable to read the value from the analog pin int servopin = 9; // Digital pin used to connect to the servo's control pin void setup() { Serial.begin(9600); myservo.attach(servopin); // Attaches the servo on servopin to the servo object } void loop() { val = analogRead(potpin); // Reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 179); // Scale "val" to use it with the servo (value between 0 and 180) // (See reference on "map" at http://arduino.cc/en/Reference/Map) // map(value, fromLow, fromHigh, toLow, toHigh); myservo.write(val); // Sets the servo position according to the scaled value // the Servo library. It goes from 0 to 180 and corresponds to the // degree of rotation. Serial.println(val); // Send the value out thru the serial port. delay(15); // Waits for the servo to get there. }