#include <Servo.h>
// Define the pin connections
const int servoPin = 9; // Servo motor control pin
const int potPin = A0; // Potentiometer analog input pin
Servo myServo; // Create a servo object
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Attach the servo to its pin
myServo.attach(servoPin);
}
void loop() {
// Read the value of the potentiometer
int potValue = analogRead(potPin);
// Map the potentiometer value to the servo angle (0-180)
int servoAngle = map(potValue, 0, 1023, 0, 180);
// Move the servo to the mapped angle
myServo.write(servoAngle);
// Print the potentiometer value and mapped angle for debugging
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" - Servo Angle: ");
Serial.println(servoAngle);
// Add a delay to stabilize readings
delay(50);
}