from py_teclado import Keypad
from py_lcd import Lcd
from machine import Pin, PWM, Timer, ADC
import time
# --- Configuración del teclado ---
teclas = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
pines_filas = [19, 18, 5, 17]
pines_columnas = [16, 4, 2, 15]
teclado = Keypad(pines_filas, pines_columnas, teclas)
# --- LCD ---
buzzer = Pin(0, Pin.OUT)
D = Lcd()
D.SetPines(12, 14, [27, 26, 25, 33])
D.Iniciar()
D.Clear()
# --- PWM del horno ---
Ppwm = Pin(21)
P = PWM(Ppwm)
P.freq(1000) # 1 kHz
P.duty(0) # inicia apagado
# --- Variables de control ---
buffer = ""
temperatura= 0
tiempo = 0
horno_encendido = False
contador_tiempo = 0 # en segundos
iniciado = False
# --- Timer ---
timer = Timer(0)
# --- ADC Temperatura ---
adc = ADC(Pin(34))
def leer_temperatura():
valor = adc.read()
# ejemplo: 0 -> 0°C, 4095 -> 500°C
temp = int((valor / 4095) * 811)
return temp
def Horno(temp):
if temp < 100:
temp = 100
if temp > 500:
temp = 500
duty = int(((temp - 100) / 400) * 823 + 200)
P.duty(duty)
def apagar_horno():
global horno_encendido
P.duty(0)
horno_encendido = False
D.Clear()
D.Print("HORNO OFF", 1, 0)
print("Horno apagado por tiempo cumplido")
def tick(timer):
global contador_tiempo, tiempo
if horno_encendido:
contador_tiempo += 1
print(f"Tiempo transcurrido: {contador_tiempo}s / {tiempo}s")
if contador_tiempo >= tiempo:
apagar_horno()
timer.deinit()
print("Sistema listo!")
ultimo_log = time.ticks_ms()
while True:
if time.ticks_diff(time.ticks_ms(), ultimo_log) >= 500:
temp_actual = leer_temperatura()
print(f"Temperatura actual: {temp_actual}°C")
ultimo_log = time.ticks_ms()
if not iniciado:
D.Print("Presione F3 para", 0, 0)
D.Print("iniciar ingreso", 1, 0)
k = teclado.get_key()
if k is not None:
print(f"La tecla es: {k}")
# beep
if k == "C": # reinicio
iniciado = True
buffer = ""
temperatura = 0
tiempo = 0
modo = None
contador_tiempo = 0
horno_encendido = False
timer.deinit()
D.Clear()
D.Print("F1) Temp.", 0, 0)
D.Print("F2) Tiempo", 1, 0)
elif iniciado:
if k.isdigit():
if len(buffer) < 3:
buffer += k
D.Print(" " * 20, 3, 0)
D.Print(buffer, 3, 0)
elif k == "*": # borrar buffer
buffer = ""
D.Print(" " * 20, 3, 0)
elif k == "A": # seleccionar modo temperatura
modo = "temp"
buffer = ""
D.Clear()
D.Print("Guardar con +",0,0)
D.Print("Temp(C):", 1, 0)
elif k == "B": # seleccionar modo tiempo
modo = "tiempo"
buffer = ""
D.Clear()
D.Print("Guardar con +",0,0)
D.Print("Tiempo(s):", 1, 0)
elif k == "#": # confirmar valor
if buffer != "" and modo == "temp":
temperatura = int(buffer)
if temperatura > 500:
temperatura = 500
D.Clear()
D.Print(f"Fijado {temperatura}C", 1, 0)
print(f"Temperatura fijada: {temperatura}°C")
buffer = ""
modo = None
elif buffer != "" and modo == "tiempo":
tiempo = int(buffer)
if tiempo > 240:
tiempo = 240
D.Clear()
D.Print(f"Fijado {tiempo}s", 2, 0)
time.sleep(2)
D.Clear()
D.Print("OK para iniciar.")
print(f"Tiempo fijado: {tiempo} segundos")
buffer = ""
modo = None
elif k == "D" and temperatura>0 and tiempo>0: # Iniciar horno (solo si temperatura alcanzada)
D.Print("Esperando Temp...", 2, 0)
Horno(temperatura)
horno_encendido = True
contador_tiempo = 0
D.Clear()
D.Print("HORNO ON", 0, 0)
print("Horno encendido")
while True:
temp_actual = leer_temperatura() #SALIDA DEL HORNO
print(f"Temperatura actual: {temp_actual:.2f} °C")
D.Print("Temp: {:.1f}C".format(temperatura), 3, 0)
if temp_actual >= temperatura: # cuando alcanza la temp
D.Clear()
D.Print("Temp alcanzada", 0, 0)
timer.init(period=1000, mode=Timer.PERIODIC, callback=tick)
if temp_actual < temperatura and temp_actual <=40:
buzzer.value(1)
time.sleep_ms(100)
buzzer.value(0)
break
time.sleep(0.5)
time.sleep_ms(50)