#include <Servo.h>
#define POTENTIOMETER_PIN A0 // Analog pin connected to the potentiometer
#define SERVO_PIN 9 // Digital pin connected to the servo motor
#define THRESHOLD 512 // Threshold value for potentiometer analog reading
Servo servo; // Define servo object
int potValue; // Variable to store potentiometer value
void setup() {
Serial.begin(9600); // Initialize serial communication
servo.attach(SERVO_PIN); // Attach servo to digital pin
}
void loop() {
potValue = analogRead(POTENTIOMETER_PIN); // Read potentiometer value
// Check if potentiometer value exceeds threshold
if (potValue > THRESHOLD) {
rotateServo(90); // Rotate servo to 90 degrees
} else {
rotateServo(0); // Rotate servo to 0 degrees
}
}
void rotateServo(int angle) {
servo.write(angle); // Set servo angle
delay(100); // Delay for servo to reach position
}