#include <ESP32Servo.h>
const int ledPin = 16;
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
const uint8_t btnPin = 2;
Servo myservo;
float pos = 0.0; // Variable where the arm's position will be stored (in degrees)
float step = 1.0; // Variable used for the arm's position step
int servoPin = 16;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
pinMode(btnPin, INPUT);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 500, 200);
}
void loop() {
// put your main code here, to run repeatedly:
delay(50); // this speeds up the simulation
for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
pos += 10;
myservo.write(pos);
Serial.println(pos);
if (pos > 120) {
pos = 0;
}
}