from time import sleep
from machine import Pin
class Key:
def __init__(self, name):
self.name = name
self.active = False
row_pins = []
col_pins = []
keys = [
[Key('1'), Key('2'), Key('3'), Key('A')],
[Key('4'), Key('5'), Key('6'), Key('B')],
[Key('7'), Key('8'), Key('9'), Key('C')],
[Key('*'), Key('0'), Key('#'), Key('D')]
]
for pin_num in range(15,11,-1):
row_pins.append(Pin(pin_num, Pin.OUT))
for pin_num in range(11,7,-1):
col_pins.append(Pin(pin_num, Pin.IN, Pin.PULL_DOWN))
def scan_keys():
# turn on each row
for row, row_pin in enumerate(row_pins):
row_pin.on()
# check each column to see if the key is pressed
# and set that key to "active" in the keys matrix
for col, col_pin in enumerate(col_pins):
keys[row][col].active = bool(col_pin.value())
# turn off the row
row_pin.off()
entered_password = '' # track user's input
correct_password = '2023'
## main loop
while True:
scan_keys()
for row in keys:
for key in row:
if key.active:
if key.name is '#': # user pressed 'enter'
print()
if entered_password == correct_password:
print("Welcome in!")
else:
print("Wrong Password")
entered_password = ''
else:
print(key.name, end='')
entered_password += key.name
# debounce button press
sleep(0.25)