from machine import Pin, I2C
import time
from keypad_utp import KeyPad
from pico_i2c_lcd import I2cLcd
# Configuración LCD
I2C_ADDR = 0x27
LCD_COLUMNS = 16
LCD_LINES = 2
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
lcd = I2cLcd(i2c, I2C_ADDR, LCD_LINES, LCD_COLUMNS)
lcd.clear()
lcd.putstr("Ingrese clave:")
lcd.move_to(0, 1)
lcd.putstr(" " * LCD_COLUMNS)
# Configuración del teclado
Teclas = [
'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D',
]
keypad = KeyPad(r1=19, r2=18, r3=5, r4=4, c1=13, c2=12, c3=14, c4=27, keys=Teclas)
keypad.start()
# LED para indicar acceso correcto
ledv = Pin(26, Pin.OUT)
ledR = Pin(25, Pin.OUT)
ledv.off()
ledR.off()
# Variables de control
clave_correcta = "3625#"
clave_ingresada = ""
intentos = 0
MAX_INTENTOS = 3
try:
while True:
tecla = keypad.get_key()
if tecla:
if tecla == '*' and len(clave_ingresada) > 0:
clave_ingresada = clave_ingresada[:-1] # Borrar el último carácter
elif len(clave_ingresada) < 6 and tecla not in ['*']: # Limitar entrada
clave_ingresada += tecla
# Actualizar solo lo ingresado sin limpiar toda la pantalla
lcd.move_to(0, 1)
lcd.putstr(clave_ingresada + " " * (LCD_COLUMNS - len(clave_ingresada)))
# Validación de clave
if tecla == '#' or len(clave_ingresada) == 6:
if clave_ingresada == clave_correcta:
lcd.clear()
lcd.putstr("Correcta")
ledv.on() # Enciende el LED al ingresar clave correcta
time.sleep(1.5)
ledv.off()
intentos = 0
else:
intentos += 1
lcd.clear()
lcd.putstr("Incorrecta")
ledR.on() # Enciende el LED al ingresar clave correcta
time.sleep(1.5)
ledR.off()
if intentos >= MAX_INTENTOS:
lcd.clear()
lcd.putstr("BLOQUEADO")
ledR.on() # Enciende el LED al ingresar clave correcta
time.sleep(5)
ledR.off()
intentos = 0
clave_ingresada = ""
lcd.clear()
lcd.putstr("Ingrese clave:")
lcd.move_to(0, 1)
lcd.putstr(" " * LCD_COLUMNS)
time.sleep(0.05)
finally:
keypad.stop()