servoVas.ino•1.03 kB
#include <ESP32Servo.h>
// Define the servo and the pin it is connected to
Servo myServo;
const int servoPin = 14;
// Define the minimum and maximum pulse widths for the servo
const int minPulseWidth = 500; // 0.5 ms
const int maxPulseWidth = 2500; // 2.5 ms
void setup() {
// Attach the servo to the specified pin and set its pulse width range
myServo.attach(servoPin, minPulseWidth, maxPulseWidth);
// Set the PWM frequency for the servo
myServo.setPeriodHertz(50); // Standard 50Hz servo
}
void loop() {
// Faster sweep from 0 to 180 degrees
for (int angle = 0; angle <= 180; angle += 2) {
int pulseWidth = map(angle, 0, 180, minPulseWidth, maxPulseWidth);
myServo.writeMicroseconds(pulseWidth);
delay(5); // Reduced delay to speed up
}
// Faster sweep from 180 to 0 degrees
for (int angle = 180; angle >= 0; angle -= 2) {
int pulseWidth = map(angle, 0, 180, minPulseWidth, maxPulseWidth);
myServo.writeMicroseconds(pulseWidth);
delay(5); // Reduced delay to speed up
}
}