// Servo Sweep example for the ESP32
// https://wokwi.com/arduino/projects/323706614646309460
#include <ESP32Servo.h>
const int servoPin = 18;
#define BUTTON_PINSW 19
#define LED_PIN 14
#define BTN_PINLED 12
Servo servo;
int brightness = 50;
void setup() {
servo.attach(servoPin, 500, 2400);
Serial.begin(115200);
pinMode(BUTTON_PINSW, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT); // configure LED pin as output
pinMode(BTN_PINLED, INPUT_PULLUP); // configure push-button pin as input with internal pull-up resistor
attachInterrupt(digitalPinToInterrupt(BTN_PINLED), increaseBrightness, FALLING);
}
int pos = 0;
int lastState = HIGH;
void loop() {
int value = digitalRead((BUTTON_PINSW));
if (value == LOW) {
swingmode();
}
if (value == HIGH) {
nomalmode();
}
analogWrite(LED_PIN, brightness * 255 / 100); // set LED brightness based on current brightness level
delay(100);
}
void swingmode(){
if (true) {
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(15);
}
}
}
void nomalmode(){
if (true) {
servo.write(90);
delay(15);
}
}
void increaseBrightness() {
brightness += 10; // increment brightness by 10%
if (brightness > 100) { // if brightness exceeds 100%, reset it to 0%
brightness = 0;
}
}