from machine import Pin, PWM, I2C
from time import sleep
from ELB import SSD1306_I2C, Servo
# Pin definitions for rows and columns of the keypad
rows = [Pin(2, Pin.OUT),
Pin(3, Pin.OUT),
Pin(4, Pin.OUT),
Pin(5, Pin.OUT)]
cols = [Pin(6, Pin.IN, Pin.PULL_UP),
Pin(7, Pin.IN, Pin.PULL_UP),
Pin(8, Pin.IN, Pin.PULL_UP),
Pin(9, Pin.IN, Pin.PULL_UP)]
# Keypad matrix (mapping rows and columns to keypad keys)
keypad = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
servo = Servo(10)
servo.set_angle(0)
buzzer = PWM(Pin(12))
buzzer.freq(1000)
correct_password = "1234"
password = ""
count = 0
# Display initial prompt (split text into two lines)
oled.fill(0)
oled.text("Enter 4-digit", 10, 10)
oled.text("password:", 10, 20)
oled.show()
while True:
for row_num, row in enumerate(rows):
row.value(0) # Activate the current row
for col_num, col in enumerate(cols):
if col.value() == 0: # Check if the key is pressed
key = keypad[row_num][col_num]
print("Key pressed:", key)
if key.isdigit():
password += key
count += 1
oled.fill(0)
# Show asterisk for each digit entered
oled.text("Enter: " + "*" * count, 10, 30)
oled.show()
sleep(0.3)
if count == 4:
if password == correct_password:
oled.fill(0)
oled.text("Access Granted!", 10, 30)
oled.show()
# Open the door
servo.set_angle(180) # Open position (180°)
buzzer.duty_u16(30000) # Start beep
sleep(0.15)
buzzer.duty_u16(0) # Stop beep
sleep(2)
# Close the door
servo.set_angle(0) # Close position (0°)
sleep(2)
else:
oled.fill(0)
oled.text("Access Denied!", 10, 30)
oled.show()
buzzer.duty_u16(30000) # Beep for denial
sleep(0.3)
buzzer.duty_u16(0)
sleep(0.3)
buzzer.duty_u16(30000)
sleep(0.3)
buzzer.duty_u16(0)
# Reset for next attempt
password = ""
count = 0
oled.fill(0)
oled.text("Enter 4-digit", 10, 10)
oled.text("password:", 10, 20)
oled.show()
row.value(1) # Deactivate the current row
sleep(0.1) # Small delay between keypad checks