#include <Servo.h>
Servo servo1; // Create servo object for first servo
Servo servo2; // Create servo object for second servo
Servo servo3; // Create servo object for third servo
Servo servo4; // Create servo object for fourth servo
#define servoPin1 2 // Pin for first servo
#define servoPin2 3 // Pin for second servo
#define servoPin3 4 // Pin for third servo
#define servoPin4 5 // Pin for fourth servo
#define pushButtonPin1 6 // Push button pin for first servo
#define pushButtonPin2 7 // Push button pin for second servo
#define pushButtonPin3 8 // Push button pin for third servo
#define pushButtonPin4 9 // Push button pin for fourth servo
int angle1 = 90; // Initial angle for first servo
int angle2 = 90; // Initial angle for second servo
int angle3 = 90; // Initial angle for third servo
int angle4 = 90; // Initial angle for fourth servo
int angleStep = 5; // Angle step
const int minAngle = 0;
const int maxAngle = 180;
bool button1Pushed = false;
bool button2Pushed = false;
bool button3Pushed = false;
bool button4Pushed = false;
void setup() {
Serial.begin(9600); // Setup serial
servo1.attach(servoPin1); // Attaches the first servo to the servo object
servo2.attach(servoPin2); // Attaches the second servo to the servo object
servo3.attach(servoPin3); // Attaches the third servo to the servo object
servo4.attach(servoPin4); // Attaches the fourth servo to the servo object
pinMode(pushButtonPin1, INPUT_PULLUP); // Set push button pin for first servo as input
pinMode(pushButtonPin2, INPUT_PULLUP); // Set push button pin for second servo as input
pinMode(pushButtonPin3, INPUT_PULLUP); // Set push button pin for third servo as input
pinMode(pushButtonPin4, INPUT_PULLUP); // Set push button pin for fourth servo as input
Serial.println("Servo Control Initialized");
}
void loop() {
// Check if push button for first servo is pressed
if (digitalRead(pushButtonPin1) == LOW) {
button1Pushed = true;
}
// Check if push button for second servo is pressed
if (digitalRead(pushButtonPin2) == LOW) {
button2Pushed = true;
}
// Check if push button for third servo is pressed
if (digitalRead(pushButtonPin3) == LOW) {
button3Pushed = true;
}
// Check if push button for fourth servo is pressed
if (digitalRead(pushButtonPin4) == LOW) {
button4Pushed = true;
}
// Handle first servo movement
if (button1Pushed) {
angle1 += angleStep;
if (angle1 >= maxAngle || angle1 <= minAngle) {
angleStep = -angleStep; // Reverse direction
}
servo1.write(angle1);
Serial.print("Servo 1 moved to: ");
Serial.print(angle1);
Serial.println(" degrees");
delay(50); // Waits for the servo to get there
button1Pushed = false; // Reset button state
}
// Handle second servo movement
if (button2Pushed) {
angle2 += angleStep;
if (angle2 >= maxAngle || angle2 <= minAngle) {
angleStep = -angleStep; // Reverse direction
}
servo2.write(angle2);
Serial.print("Servo 2 moved to: ");
Serial.print(angle2);
Serial.println(" degrees");
delay(50);
button2Pushed = false; // Reset button state
}
// Handle third servo movement
if (button3Pushed) {
angle3 += angleStep;
if (angle3 >= maxAngle || angle3 <= minAngle) {
angleStep = -angleStep; // Reverse direction
}
servo3.write(angle3);
Serial.print("Servo 3 moved to: ");
Serial.print(angle3);
Serial.println(" degrees");
delay(50);
button3Pushed = false; // Reset button state
}
// Handle fourth servo movement
if (button4Pushed) {
angle4 += angleStep;
if (angle4 >= maxAngle || angle4 <= minAngle) {
angleStep = -angleStep; // Reverse direction
}
servo4.write(angle4);
Serial.print("Servo 4 moved to: ");
Serial.print(angle4);
Serial.println(" degrees");
delay(50);
button4Pushed = false; // Reset button state
}
}