#include <Servo.h>
// Define the Servo object
Servo myServo;
// Define the analog pin for reading voltage
const int analogPin = A5;
// Define the pin to which the Servo is connected
const int servoPin = 9;
void setup() {
// Attach the Servo to the specified pin
myServo.attach(servoPin);
// Begin serial communication for debugging purposes
Serial.begin(9600);
}
void loop() {
// Read the voltage from the analog pin
int analogValue = analogRead(analogPin);
// Convert the analog value (0-1023) to a voltage (0-5V)
float voltage = analogValue * (5.0 / 1023.0);
// Map the analog value (0-1023) to a Servo angle (0-180)
int servoAngle = map(analogValue, 0, 1023, 0, 180);
// Set the Servo angle
myServo.write(servoAngle);
// Print the voltage and Servo angle for debugging
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V, Servo Angle: ");
Serial.println(servoAngle);
// Small delay for stability
delay(100);
}