#include <ESP32Servo.h>
class ServoMotor {
private:
Servo servo;
int pin;
public:
ServoMotor(int servoPin) : pin(servoPin) {
servo.attach(pin);
}
void setPosition(int angle) {
servo.write(angle);
delay(5); // Delay to allow the servo to reach the desired position
}
};
ServoMotor servo1(2); // Pin 2 is used for first servo motor
ServoMotor servo2(4); // Pin 3 is used for second servo motor
const int button1Pin = 26; // Pin connected to the first push button
const int button2Pin = 27; // Pin connected to the second push button
int button1State = 0; // Variable to store the state of button 1
int button2State = 0; // Variable to store the state of button 2
void setup() {
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
}
void loop(){
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// If button 1 is pressed, sweep servo 1
if (button1State == HIGH && button2State == HIGH) {
for (int angle = 0; angle <= 180; angle += 1) {
servo1.setPosition(angle);
servo2.setPosition(angle);
}
}
}