#include <Servo.h>
Servo myservo; // Create a Servo object
int buttonPin = 2; // Pin connected to push button
int servoPos = 0; // Variable to store the position of the servo
bool isOpen = false; // Variable to track if the door is open or closed
void setup() {
myservo.attach(9); // Attach the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // Check if the button is pressed (LOW because of INPUT_PULLUP)
delay(0); // Debouncing delay
if (isOpen) {
// Close the servo (move to 0 degrees)
myservo.write(0);
isOpen = false;
} else {
// Open the servo (move to 180 degrees)
myservo.write(90);
isOpen = true;
}
delay(1000); // Wait for 1 second to prevent multiple presses
}
}