from machine import Pin, I2C
from utime import sleep
from pico_i2c_lcd import I2cLcd
led_verde = Pin(28, Pin.OUT)
led_rojo = Pin(21, Pin.OUT)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
filas = [
Pin(13, Pin.OUT),
Pin(12, Pin.OUT),
Pin(11, Pin.OUT),
Pin(10, Pin.OUT)
]
columnas = [
Pin(9, Pin.IN, Pin.PULL_DOWN),
Pin(8, Pin.IN, Pin.PULL_DOWN),
Pin(7, Pin.IN, Pin.PULL_DOWN),
Pin(6, Pin.IN, Pin.PULL_DOWN)
]
teclas = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
def leer_teclado():
for f in range(4):
filas[f].value(1)
for c in range(4):
if columnas[c].value() == 1:
filas[f].value(0)
sleep(0.25)
return teclas[f][c]
filas[f].value(0)
return None
lcd.clear()
lcd.putstr("Ingrese codigo")
while True:
tecla = leer_teclado()
if tecla != None:
lcd.clear()
# MOSTRAR TECLA EN PRIMERA LÍNEA
lcd.move_to(0, 0)
lcd.putstr("Tecla: " + tecla)
# VALIDACIÓN DEL CÓDIGO EN SEGUNDA LÍNEA
lcd.move_to(1, 0)
if tecla == "8": # Código correcto
lcd.putstr("Codigo correcto")
led_rojo.off()
led_verde.on()
sleep(5)
led_verde.off()
else: # Código incorrecto
lcd.putstr("Codigo erroneo")
led_verde.off()
led_rojo.on()
sleep(1)
lcd.clear()
lcd.putstr("Ingrese codigo")
else:
led_verde.off()
led_rojo.on()