from gpiozero import Button, LED
from time import sleep
UNLOCK_SEQUENCE = [2, 3, 1, 3]
ADMIN_SEQUENCE = [3, 1]
BTN_PINS = [17, 27, 22, 23] # Replace with your actual GPIO pin numbers
LED_PINS = [24, 25, 5, 6, 16, 20] # Replace with your actual GPIO pin numbers
ALARM_LED_PIN = 12 # Replace with your actual GPIO pin number for the alarm LED
UNLOCKED_LED_PIN = 13 # Replace with your actual GPIO pin number for the unlocked LED
is_locked = True
alarm_triggered = False
buttons = [Button(pin) for pin in BTN_PINS]
leds = [LED(pin) for pin in LED_PINS]
alarm_led = LED(ALARM_LED_PIN)
unlocked_led = LED(UNLOCKED_LED_PIN)
def update_leds():
unlocked_led.value = not is_locked
def check_sequence(sequence):
global is_locked, alarm_triggered
if sequence == UNLOCK_SEQUENCE:
if not is_locked:
print("Lock closed.")
is_locked = True
update_leds()
else:
print("UNLOCKED! Lock open.")
is_locked = False
update_leds()
elif sequence == ADMIN_SEQUENCE and alarm_triggered:
print("Alarm deactivated. Resetting...")
alarm_triggered = False
alarm_led.off()
elif sequence != UNLOCK_SEQUENCE and sequence != ADMIN_SEQUENCE:
if sequence != [3, 3, 1, 1]: # Skip Reset State scenario
alarm_triggered = True
print("ALARM triggered!")
# Your alarm signal logic here
alarm_led.on()
def button_pressed(button, led):
led.on()
sleep(0.2) # Debounce delay (adjust as needed)
return button.is_pressed
while True:
if is_locked or alarm_triggered:
for button, led in zip(buttons, leds):
if button_pressed(button, led):
sequence = [buttons[i].is_pressed for i in range(len(buttons))]
check_sequence(sequence)
led.off()
else:
print("Press any key to close the lock.")
# Read button inputs here and set is_locked to True when any button is pressed
# For example: if any(button.is_pressed for button in buttons): is_locked = True