from machine import Pin, I2C
import utime
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from keypad import Keypad
# === LCD Setup ===
I2C_ADDR = 0x27 # Default I2C address for LCD (may vary)
i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# === LEDs Setup ===
led1 = Pin(21, Pin.OUT) # Wrong password LED
led2 = Pin(22, Pin.OUT) # Correct password LED
# === Button Setup ===
reset_button = Pin(0, Pin.IN, Pin.PULL_DOWN)
# === Keypad Setup ===
ROWS = [20, 19, 18, 15]
COLS = [14, 13, 2, 1]
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
keypad = Keypad(row_pins=ROWS, col_pins=COLS, keys=keys)
# === Password Setup ===
correct_password = "1234"
input_password = ""
password_entered = False
# === Main Loop ===
while True:
if not password_entered:
key = keypad.get_key()
if key:
if key == '#': # Submit password
if input_password == correct_password:
lcd.clear()
lcd.putstr("CORRECT PASSWORD")
lcd.move_to(0, 1)
lcd.putstr("DOOR UNLOCK")
led2.value(1)
led1.value(0)
else:
lcd.clear()
lcd.putstr("WRONG PASSWORD")
lcd.move_to(0, 1)
lcd.putstr("DOOR LOCK")
led1.value(1)
led2.value(0)
password_entered = True
elif key == '*': # Clear current input
input_password = ""
lcd.clear()
else:
input_password += key
lcd.move_to(0, 0)
lcd.putstr("*" * len(input_password))
utime.sleep(0.1)
else:
if reset_button.value() == 1:
input_password = ""
password_entered = False
lcd.clear()
led1.value(0)
led2.value(0)
time.sleep(0.5) # debounce delay