#include <Servo.h>
Servo lockServo; // Create a servo object
int unlockAngle = 90; // Angle to unlock the door
int lockAngle = 0; // Angle to lock the door
int buttonPin = 2; // Pin for the button
int ledPin = 13; // Pin for the LED
bool locked = true; // Initial lock state
void setup() {
lockServo.attach(9); // Attach the servo to pin 9
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as output
digitalWrite(ledPin, LOW); // Turn off LED initially
}
void loop() {
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// Toggle lock state
locked = !locked;
// Actuate the servo motor accordingly
if (locked) {
lockServo.write(lockAngle); // Lock the door
digitalWrite(ledPin, LOW); // Turn off LED
} else {
lockServo.write(unlockAngle); // Unlock the door
digitalWrite(ledPin, HIGH); // Turn on LED
}
// Add a delay to avoid multiple rapid button presses
delay(500);
}
}