import machine
import time
# Configuración de los pines (ajustados según la imagen de Wokwi)
# Filas (Rows) -> Pins GP2, GP3, GP4, GP5 (Cables verdes)
# Columnas (Cols) -> Pins GP6, GP7, GP8, GP9 (Cables azules)
filas_pines = [2, 3, 4, 5]
columnas_pines = [6, 7, 8, 9]
# Definición de las teclas
teclas = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# Configuración de hardware
filas = [machine.Pin(pin_nombre, machine.Pin.OUT) for pin_nombre in filas_pines]
columnas = [machine.Pin(pin_nombre, machine.Pin.IN, machine.Pin.PULL_DOWN) for pin_nombre in columnas_pines]
PASSWORD_CORRECTA = "1706"
password_introducida = ""
print("Esperando contraseña...")
def leer_teclado():
for f in range(4):
filas[f].value(1)
for c in range(4):
if columnas[c].value() == 1:
tecla = teclas[f][c]
while columnas[c].value() == 1: # Debounce simple
time.sleep(0.01)
filas[f].value(0)
return tecla
filas[f].value(0)
return None
while True:
tecla = leer_teclado()
if tecla:
if tecla == '#': # Usamos '#' como tecla para borrar o resetear
print("Reset manual")
password_introducida = ""
elif tecla == '*': # Usamos '*' como tecla de Enter/Validar
if password_introducida == PASSWORD_CORRECTA:
print("--- ACCESO CONCEDIDO ---")
else:
print("--- ACCESO DENEGADO ---")
password_introducida = "" # Limpiar después de intentar
else:
password_introducida += tecla
print("Dígito: " + tecla)
# Validación automática al llegar a 4 dígitos
if len(password_introducida) == 4:
time.sleep(0.2)
if password_introducida == PASSWORD_CORRECTA:
print(">> Contraseña Correcta: ACCESO CONCEDIDO")
else:
print(">> ERROR: ACCESO DENEGADO")
password_introducida = "" # Reiniciar para el siguiente intento
time.sleep(0.1)