// Constants
const int potPin = A0; // Analog pin where potentiometer is connected
const int servoPin = 9; // PWM pin where servo motor is connected
// Variables
int potValue = 0; // Variable to store the input from the potentiometer
int servoAngle = 0; // Variable to store the servo position
void setup()
{
// Start serial communication at 9600 baud
Serial.begin(9600);
}
void loop()
{
// Read the value from the potentiometer
potValue = analogRead(potPin);
// Map the potentiometer value to an angle between 0 and 180 degrees
servoAngle = map(potValue, 0, 255, 0, 180);
// Set the servo position
set_servo_angle(servoAngle);
// Print the potentiometer value and the servo angle to the serial monitor
// Wait a bit before the next read
delay(10);
}
void set_servo_angle(char angle)
{
Serial.print("Angle:");
Serial.println(angle);
int data=map(angle,0,180,500,2400);
digitalWrite(9,1);
delayMicroseconds(data);
digitalWrite(9,0);
delayMicroseconds(20000-data);
}