from machine import Pin
import time
# Define keypad rows and columns
keypad_rows = [32, 33, 25, 26] # Define GPIO pins for keypad rows
keypad_cols = [27, 14, 12, 13] # Define GPIO pins for keypad columns
# Keypad keys mapping
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# Password to compare
password = ['1', '2', '3', '4'] # Change this to your desired 4-digit password
# Initialize the keypad pins
row_pins = [Pin(row, Pin.OUT) for row in keypad_rows]
col_pins = [Pin(col, Pin.IN, Pin.PULL_UP) for col in keypad_cols]
def get_key_pressed():
for i, row_pin in enumerate(row_pins):
row_pin.off() # Activate the row
for j, col_pin in enumerate(col_pins):
if not col_pin.value(): # Check column for a pressed key
row_pin.on() # Deactivate the row
return keys[i][j] # Return the corresponding key
row_pin.on() # Deactivate the row after checking
return None # Return None if no key is pressed
def enter_password():
entered_password = []
while len(entered_password) < 4:
key = get_key_pressed()
if key:
entered_password.append(key)
print("Entered:", "".join(entered_password)) # Display entered digits
time.sleep(0.2) # Small delay between key presses
return entered_password
# Main logic
while True:
print("Enter 4-digit password:")
entered_code = enter_password()
if entered_code == password:
print("Yes")
break
else:
print("Try again")