#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potentiometerPin = A0; // analog input pin for the potentiometer
int potValue = 0; // variable to store the potentiometer value
int servoPos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); // initialize serial communication
}
void loop() {
potValue = analogRead(potentiometerPin); // read the potentiometer value (0-1023)
// Map the potentiometer value to the servo's angle range (0-180 degrees)
servoPos = map(potValue, 0, 1023, 0, 180);
myservo.write(servoPos); // move the servo to the mapped position
// Print the corresponding position of the servo based on the potentiometer value
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" Servo Position: ");
Serial.println(servoPos);
delay(15); // a small delay for stability
}