25 lines
1.3 KiB
Arduino
25 lines
1.3 KiB
Arduino
|
#include <ESP32Servo.h>
|
||
|
|
||
|
Servo myservo; // create servo object to control a servo
|
||
|
int servo_angle = 0; // variable to store the servo position
|
||
|
#define SERVOPIN 14 // Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
|
||
|
|
||
|
void setup() {
|
||
|
// put your setup code here, to run once:
|
||
|
myservo.attach(SERVOPIN); // attaches the servo on pin 18 to the servo object
|
||
|
// using default min/max of 1000us and 2000us
|
||
|
// different servos may require different min/max settings
|
||
|
// for an accurate 0 to 180 sweep
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
for (servo_angle = 0; servo_angle <= 180; servo_angle += 10) { // goes from 0 degrees to 180 degrees in steps of 1 degree
|
||
|
myservo.write(servo_angle); // tell servo to go to position in variable 'pos'
|
||
|
delay(50); // waits 15ms for the servo to reach the position
|
||
|
}
|
||
|
for (servo_angle = 180; servo_angle >= 0; servo_angle -= 10) { // goes from 180 degrees to 0 degrees
|
||
|
myservo.write(servo_angle); // tell servo to go to position in variable 'pos'
|
||
|
delay(50); // waits 15ms for the servo to reach the position
|
||
|
}
|
||
|
}
|