from machine import Pin
import utime
keys = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
rows = [9, 8, 7, 6]
cols = [5, 4, 3, 2]
row_pins = [Pin(r, Pin.OUT) for r in rows]
col_pins = [Pin(c, Pin.IN, Pin.PULL_DOWN) for c in cols]
guess, secret_pin = [], ['9', '6', '9', '6', '9', '6']
led = Pin(15, Pin.OUT)
def scan_keys():
for r in range(4):
row_pins[r].high()
for c in range(4):
if col_pins[c].value():
print("Pressed:", keys[r][c])
guess.append(keys[r][c])
utime.sleep(0.3)
if len(guess) == 6:
check_pin(guess)
guess.clear()
row_pins[r].low()
def check_pin(guess):
if guess == secret_pin:
print("Access granted!")
led.on(); utime.sleep(2); led.off()
else:
print("Access denied!")
print("Enter secret PIN")
while True:
scan_keys()