from machine import Pin, ADC
import time
from jr_lcd import LCD  
from jrc_teclado import JrTeclado  

# Inicializar LCD
lcd = LCD()
lcd.SetPines(rs_pin=12, e_pin=14, data_pins=[27, 26, 25, 33])  
lcd.Iniciar()
lcd.Clear()

# Inicializar Teclado
teclado = JrTeclado()
teclado.SetPines(fil_pins=[19, 18, 5, 17], col_pins=[16, 4, 2, 15])  

# Pines de control y ADC
control = Pin(32, Pin.OUT)
adc = ADC(Pin(34))

# Variables utiles
tempdes = None  
referencia = 0
salida= 0
stop = None

# Caracterizacion del sensor
def calculo(tempdes):
    m=36.386
    b=2.7143
    return round(m * tempdes + b)
def calculoinv(salida):
    m=36.386
    b=2.7143
    return round((salida-b)/m)

#Inicia el código
while True:
    num_str = ""  
    lcd.Clear()
    lcd.Print("Temperatura: ", fil=0, col=0)
    
    while True:
        tecla = teclado.EsperaTecla() 
        
        if tecla.isdigit() and len(num_str) < 2:  
            num_str += tecla
            lcd.Clear()
            lcd.Print("Temperatura: " + num_str, fil=0, col=0)  
            time.sleep(0.3) 

        elif tecla == "D":  
            if num_str:  
                tempdes = int(num_str)
                if tempdes < 10 or tempdes > 60:
                    lcd.Clear()
                    lcd.Print("Fuera del rango! ", fil=0, col=0)
                    tempdes=None
                    num_str=""
                    time.sleep(1)
                else:
                    lcd.Clear()
                    lcd.Print("Procesando...", fil=0, col=0)
                    time.sleep(1)
                    referencia = calculo(tempdes)
                    lcd.Clear()
                    lcd.Print("Temp ingresada: "+ num_str, fil=0, col=0)
                    num_str=""

                    # Generar pulsos para aumentar la temperatura
                    while True:
                        salida=adc.read()
                        lcd.Print("Temp actual: "+ str(calculoinv(salida)), fil=1, col=0)
                        if salida > referencia:
                            control.value(0)
                        else:
                            control.value(1)
                            time.sleep(0.1)

                        
                        # Habilitar paro
                        stop= teclado.GetTecla()
                        if stop == "#":
                            control.value(0)
                            lcd.Clear
                            lcd.Print("Reiniciando...", fil=0, col=0)
                            time.sleep(1)
                            referencia=0
                            salida=0
                            break  # Sale del bucle y permite ingresar otra temperatura

        elif tecla == "#" or stop == "#":  # Si se presiona "#", reiniciar el ingreso de temperatura
            lcd.Clear
            lcd.Print("Reiniciando...", fil=0, col=0)
            time.sleep(1)
            referencia=0
            salida=0
            stop=None
Aire AcondicionadoBreakout