YourESP32 Sketch Servo Sweep
Jump to navigation
Jump to search
/* YourDuinoStarter Example: ESP32 Servo Test Sweep - WHAT IT DOES Uses ESP32 to run a servo - SEE the comments after "//" on each line below - CONNECTIONS: - Servo to Pin 13 - modified for the ESP32 on March 2017 by John Bennett - V1.00 03/30/2021 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ #include <ESP32Servo.h> /*-----( Declare Constants and Pin Numbers )-----*/ #define servoPin 13 #define ServoMIN 30 // Don't go to ends of servo travel #define ServoMAX 150 // which may be 0 to 180. Experiment later for your servo. /*-----( Declare objects )-----*/ Servo myservo; // create servo object to control a servo // 16 servo objects can be created on the ESP32 /*-----( Declare Variables )-----*/ int servoPosition; // Hold the current servo angle void setup() /****** SETUP: RUNS ONCE ******/ { // Allow allocation of all timers ESP32PWM::allocateTimer(0); ESP32PWM::allocateTimer(1); ESP32PWM::allocateTimer(2); ESP32PWM::allocateTimer(3); myservo.setPeriodHertz(50); // standard 50 hz servo myservo.attach(servoPin, 500, 2400); // 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 }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { for(servoPosition = ServoMIN; servoPosition < ServoMAX; servoPosition += 1) // goes from near 0 degrees to near 180 degrees { // in steps of 1 degree myservo.write(servoPosition); // tell servo to go to position in variable 'pos' delay(10); // waits 10ms for the servo to reach the position } delay(1000); for(servoPosition = ServoMAX; servoPosition>=ServoMIN; servoPosition-=1) // goes from near 180 degrees to near 0 degrees { myservo.write(servoPosition); // tell servo to go to position in variable 'pos' delay(10); // waits 10ms for the servo to reach the position } delay(2500); // Wait, then cycle again. }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ //NONE //*********( THE END )***********