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)
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()
row.value(1) # Deactivate the current row
sleep(0.1)