#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Create an LCD object using I2C address (0x27 is the default address for most LCDs)
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo lockServo; // Create a Servo object to control the servo motor
int buttonPin = 2; // Pin where the pushbutton is connected
int ledPin = 13; // Pin where the LED is connected
int buttonState = 0; // Variable to store the pushbutton state
int lockPosition = 0; // Variable to store the current position of the lock
int buzzer=10;
void setup() {
lockServo.attach(9); // Attach the servo control pin to pin 9
pinMode(buttonPin, INPUT); // Set the button pin as input
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(buzzer, OUTPUT); // Set the LED pin as output
lockServo.write(lockPosition); // Initially, set the lock to unlocked (0°)
digitalWrite(ledPin, LOW); // Turn off the LED initially
digitalWrite(buzzer, LOW);
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.setBacklight(1); // Turn on the LCD backlight
lcd.print("Lock Status:"); // Print the label
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the state of the button
if (buttonState == HIGH) { // If the button is pressed
if (lockPosition == 0) {
lockPosition = 90; // Lock the mechanism (90°)
digitalWrite(ledPin, HIGH); // Turn the LED on (locked state)
digitalWrite(buzzer, HIGH);
lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print("Locked "); // Display "Locked"
} else {
lockPosition = 0; // Unlock the mechanism (0°)
digitalWrite(ledPin, LOW); // Turn the LED off (unlocked state)
digitalWrite(buzzer, LOW);
lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print("Unlocked "); // Display "Unlocked"
}
lockServo.write(lockPosition); // Rotate the servo to the new position
delay(500); // Debounce delay for the button press
}
}