// Servo Sweep example for the ESP32
// https://wokwi.com/arduino/projects/323706614646309460
#include <ESP32Servo.h>
Servo myServo;
const int buttonPin = 4;
const int redLed = 18;
const int greenLed = 19;
const int servoPin = 13;
bool isActive = false; // This tracks if the "trapdoor" is open or closed
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
myServo.attach(servoPin);
// Initialize: Closed state
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
myServo.write(0);
}
void loop() {
// Check if button is pressed
if (digitalRead(buttonPin) == LOW) {
isActive = !isActive; // Flip the state (if it was false, it's now true)
if (isActive) {
// Action: Open
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
myServo.write(90);
} else {
// Action: Close
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
myServo.write(0);
}
delay(300); // "Debounce" delay to prevent multiple triggers from one click
}
}