from machine import Pin, I2C, PWM
from time import sleep
from i2c_lcd import I2cLcd
from keypad import Keypad
# Inicializar LCD
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Inicializar Servo (pin 18)
servo = PWM(Pin(18), freq=50)
def mover_servo(pos):
duty = int((pos / 180 * 2 + 0.5) / 20 * 1023)
servo.duty(duty)
def abrir():
mover_servo(90)
sleep(1)
mover_servo(0)
def cerrar():
mover_servo(0)
sleep(1)
# Inicializar teclado
row_pins = [Pin(13), Pin(12), Pin(14), Pin(27)]
col_pins = [Pin(26), Pin(25), Pin(33), Pin(32)]
keys = [
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
keypad = Keypad(row_pins, col_pins, keys)
# Clave del dueño
clave_dueno = "5781DD"
def mensaje_inicio():
lcd.clear()
lcd.putstr("Bingo Mirador")
lcd.move_to(0,1)
lcd.putstr("Presione tecla")
def pedir_clave_cliente():
lcd.clear()
lcd.putstr("Clave: 4 digitos")
lcd.move_to(0, 1)
lcd.putstr("Presione * luego")
# Bucle principal
while True:
clave_cliente = ""
estado_abierto = False
mensaje_inicio()
# Esperar primera tecla
while True:
tecla = keypad.get_key()
if tecla:
break
# Ingreso de nueva clave
pedir_clave_cliente()
clave_ingresada = ""
pantalla_pos = 0
# Limpiar línea 2 antes de ingresar clave
lcd.move_to(0, 1)
lcd.putstr(" " * 16)
lcd.move_to(0, 1)
while True:
tecla = keypad.get_key()
if tecla:
if tecla == '*':
if len(clave_ingresada) == 4:
clave_cliente = clave_ingresada
lcd.clear()
lcd.putstr("Cerrando...")
cerrar()
estado_abierto = False
sleep(2)
break
else:
lcd.clear()
lcd.putstr("Clave invalida")
sleep(2)
pedir_clave_cliente()
clave_ingresada = ""
pantalla_pos = 0
lcd.move_to(0, 1)
lcd.putstr(" " * 16)
lcd.move_to(0, 1)
elif tecla.isdigit() and len(clave_ingresada) < 4:
clave_ingresada += tecla
lcd.move_to(pantalla_pos, 1)
lcd.putstr("*")
pantalla_pos += 1
# Esperar intento de apertura
while True:
lcd.clear()
lcd.putstr("Ingrese clave")
lcd.move_to(0,1)
lcd.putstr("y * para abrir")
clave_ingresada = ""
pantalla_pos = 0
lcd.move_to(0, 1)
lcd.putstr(" " * 16)
lcd.move_to(0, 1)
while True:
tecla = keypad.get_key()
if tecla:
if tecla == '*':
if clave_ingresada == clave_cliente or clave_ingresada == clave_dueno:
lcd.clear()
lcd.putstr("Abriendo...")
abrir()
sleep(2)
clave_cliente = "" # olvidar la clave del cliente
estado_abierto = True
mensaje_inicio()
while True:
tecla = keypad.get_key()
if tecla:
break
break
else:
lcd.clear()
lcd.putstr("Clave incorrecta")
sleep(2)
break
elif len(clave_ingresada) < 6:
clave_ingresada += tecla
lcd.move_to(pantalla_pos, 1)
lcd.putstr("*")
pantalla_pos += 1
if estado_abierto:
break