#include <Servo.h> // Include the Servo library to control servo motors
// Create Servo objects for each of the 4 servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
// Define analog input pins where potentiometers are connected
const int pot1 = A0; // Potentiometer 1 connected to analog pin A0
const int pot2 = A1; // Potentiometer 2 connected to analog pin A1
// Define digital pins where servos signal wires are connected
const int servoPin1 = 2; // Servo 1 connected to digital pin 2
const int servoPin2 = 3; // Servo 2 connected to digital pin 3
const int servoPin3 = 6; // Servo 3 connected to digital pin 6
const int servoPin4 = 10; // Servo 4 connected to digital pin 10
void setup() {
// Attach each servo object to its respective pin
servo1.attach(servoPin1);
servo2.attach(servoPin2);
servo3.attach(servoPin3);
servo4.attach(servoPin4);
// Initialize all servos to the neutral (center) position at 90 degrees
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
}
void loop() {
// Read the analog value from potentiometer 1 (range: 0 to 1023)
int val1 = analogRead(pot1);
// Read the analog value from potentiometer 2 (range: 0 to 1023)
int val2 = analogRead(pot2);
// Map potentiometer 1's value from 0-1023 to servo angle 0-180 degrees
int angle1 = map(val1, 0, 1023, 0, 180);
// Map potentiometer 2's value from 0-1023 to servo angle 0-180 degrees
int angle2 = map(val2, 0, 1023, 0, 180);
// Write the mapped angle to servo 1 (controlled by potentiometer 1)
servo1.write(angle1);
// Write the same mapped angle to servo 2 (also controlled by potentiometer 1)
servo2.write(angle1);
// Write the mapped angle to servo 3 (controlled by potentiometer 2)
servo3.write(angle2);
// Write the same mapped angle to servo 4 (also controlled by potentiometer 2)
servo4.write(angle2);
// Small delay to allow servos to move smoothly before next reading
delay(20);
}