#include <Servo.h> // Include Servo library
Servo myServo; // Create a servo objexct to control the servo
int servoPin = 9; // Servo motor is connected to pin 9
int potPin = A0; // potentiometer is connected to A0
int potentiometerValue; // variable to store potentiometer reading
int servoPosition; // This will store an angle for the servo arm
void setup() {
myServo.attach(servoPin); // Attach the servo motor to pin 9
Serial.begin(9600);
}
void loop() {
potentiometerValue = analogRead(potPin);
// read the value from the potentiometer - range (0-1023)
servoPosition = map(potentiometerValue, 0, 1023, 0, 180);
// changes the 0-1023 reading to a 0-180 degree scale
myServo.write(servoPosition);
// move servo arm to the calculated position (angle)
Serial.print(potentiometerValue);
Serial.print(" ");
Serial.println(servoPosition);
delay(100);
}