#include <ESP32Servo.h>
Servo myservo1; // create first servo object to control a servo
Servo myservo2; // create second servo object to control another servo
int pos = 0; // variable to store the servo position
int servoPin1 = 2; // pin for first servo
int servoPin2 = 4; // pin for second servo
void setup() {
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
// Set period and attach first servo
myservo1.setPeriodHertz(50); // standard 50 hz servo
myservo1.attach(servoPin1, 500, 2400); // attaches the first servo to its pin
// Set period and attach second servo
myservo2.setPeriodHertz(50); // standard 50 hz servo
myservo2.attach(servoPin2, 500, 2400); // attaches the second servo to its pin
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos); // tell first servo to go to position in variable 'pos'
myservo2.write(pos); // tell second servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servos to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo1.write(pos); // tell first servo to go to position in variable 'pos'
myservo2.write(pos); // tell second servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servos to reach the position
}
}