#include <ESP32Servo.h>
#include <driver/ledc.h>
#include <LedControl.h>
Servo myservo;
int servoPin = 12;
#define LED 22
#define BTN1 18
#define BTN2 19
#define BTN3 21
#define BTN0 5
#define BTNSpin 33
// setting PWM properties
const int freq = 1000;
const int ledChannel = 0;
const int resolution = 8;
int dutyCycle;
int pos = 0;
void setup() {
pinMode(BTN0, INPUT); // btn0 pin to input mode
pinMode(BTN1, INPUT); // btn1 pin to input mode
pinMode(BTN2, INPUT); // btn2 pin to input mode
pinMode(BTN3, INPUT); // btn3 pin to input mode
pinMode(BTNSpin, INPUT); //btnspin pin to input mode
// attach the channel to the GPIO to be controlled
ledcAttachPin(LED, ledChannel);
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
myservo.attach(servoPin);
}
void loop() {
if(digitalRead(BTN0)==HIGH) {
digitalWrite(BTNSpin, LOW);
dutyCycle = 0;
ledcWrite(ledChannel, dutyCycle);
}
if(digitalRead(BTN1)==HIGH) {
dutyCycle = 85;
ledcWrite(ledChannel, dutyCycle);
}
if(digitalRead(BTN2)==HIGH) {
dutyCycle = 170;
ledcWrite(ledChannel, dutyCycle);
}
if(digitalRead(BTN3)==HIGH) {
dutyCycle = 255;
ledcWrite(ledChannel, dutyCycle);
}
if(digitalRead(BTNSpin)==HIGH) {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
}