#include Servo myServo; int servoPin = D0; int buttonPin = D3; int ledPin = D2; bool state = false; // general state bool lastButtonState = HIGH; void setup() { myServo.attach(servoPin); pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); } void loop() { bool reading = digitalRead(buttonPin); // Detect button press (falling edge) if (lastButtonState == HIGH && reading == LOW) { state = !state; // toggle state if (state) { myServo.write(180); digitalWrite(ledPin, HIGH); } else { myServo.write(0); digitalWrite(ledPin, LOW); } delay(200); // simple debounce } lastButtonState = reading; }