from machine import Pin
from time import sleep
# Mapeamento do teclado
teclas = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
linhas_pinos = [7, 6, 5, 4]
colunas_pinos = [3, 2, 1, 0]
linhas = [Pin(x, Pin.OUT) for x in linhas_pinos]
colunas = [Pin(i, Pin.IN, Pin.PULL_DOWN) for i in colunas_pinos]
led = Pin(28, Pin.OUT) # Exemplo de pino do LED
senha_correta = ['1', '2', '3']
entrada = []
def teclado():
for z in range(4):
for linha in linhas:
linha.value(0)
linhas[z].value(1)
for a in range(4):
if colunas[a].value() == 1:
while colunas[a].value() == 1:
sleep(0.01)
return teclas[z][a]
return None
def cofre():
global entrada
tecla = teclado()
if tecla:
print(f"Tecla: {tecla}")
entrada.append(tecla)
# Limita o tamanho da entrada
if len(entrada) > 3:
entrada = entrada[-3:]
if entrada == senha_correta:
print("Senha correta! Cofre aberto!")
led.value(1)
sleep(2)
led.value(0)
entrada = [] # reseta
else:
print(f"Entrada atual: {entrada}")
while True:
cofre()