from machine import Pin, ADC, Timer
import time
#botones
UP = Pin(0, Pin.IN, Pin.PULL_UP) # aumentar temperatura
DOWN = Pin(1, Pin.IN, Pin.PULL_UP) #disminuir temperatura
SET = Pin(15, Pin.IN, Pin.PULL_UP) #configurar temperatura
#tierras para el display
T1 = Pin(11, Pin.OUT) #unidades
T2 = Pin(12, Pin.OUT) #decenas
T3 = Pin(13, Pin.OUT) #decenas
T4 = Pin(14, Pin.OUT) #decenas
#led
LED = Pin(10, Pin.OUT)
# Salidas para los segmentos
leds = ( 2, 3, 4, 5, 6, 7, 8, 9)
lista_Pin = list()
for i in range(8):
    lista_Pin.append(Pin(leds[i], Pin.OUT))

digitos = {
   #1  2  3  4  5  6  7  8
0: [1, 1, 1, 1, 1, 0, 1, 0], # 0
1: [0, 0, 1, 0, 0, 0, 1, 0], # 1
2: [1, 0, 1, 1, 1, 0, 0, 1], # 2
3: [1, 0, 1, 0, 1, 0, 1, 1], # 3
4: [0, 1, 1, 0, 0, 0, 1, 1], # 4
5: [1, 1, 0, 0, 1, 0, 1, 1], # 5
6: [1, 1, 0, 1, 1, 0, 1, 1], # 6
7: [1, 0, 1, 0, 0, 0, 1, 0], # 7
8: [1, 1, 1, 1, 1, 0, 1, 1], # 8
9: [1, 1, 1, 0, 0, 0, 1, 1] # 9
}

def mostrar(numero):
    for pin, estado in zip(lista_Pin, digitos[numero]):
        pin.value(estado)
        
def aumentar(UP):
    global temperatura,flag,temperaturaideal
    if (flag==(-1)):
        temperaturaideal = temperaturaideal + 0.5

def disminuir(DOWN):
    global temperatura,temperaturaideal
    if (flag==(-1)):
        temperaturaideal = temperaturaideal - 0.5
        
    
    
def cambiar(SET):
    global flag
    flag= flag * -1
    
def funcion():
    global temperatura,temperaturaideal
    tiempo = 5
    milesismas = (int(temperaturaideal * 100))%10
    centecimas = (int(temperaturaideal * 10))%10
    unidades = int(temperaturaideal%10)
    decenas = int(temperaturaideal/10)
    ####mosrar los numeros
    for pin, estado in zip(lista_Pin, digitos[milesismas]):
        pin.value(estado)
    T4.value(0)
    time.sleep_ms(tiempo)
    T4.value(1)
    for pin, estado in zip(lista_Pin, digitos[centecimas]):
        pin.value(estado)
    T3.value(0)
    time.sleep_ms(tiempo)
    T3.value(1)
    cont=2 #Variable para saber el pin que se esta encendiendo
    for pin, estado in zip(lista_Pin, digitos[unidades]):
        if(cont==7):
            pin.value(1) #para encender solo el punto
        else:
            pin.value(estado)
        cont=cont+1
    T2.value(0)
    time.sleep_ms(tiempo)
    T2.value(1)
    for pin, estado in zip(lista_Pin, digitos[decenas]):
        pin.value(estado)
    T1.value(0)
    time.sleep_ms(tiempo)
    T1.value(1)

 
UP.irq(trigger=Pin.IRQ_FALLING, handler=aumentar)
DOWN.irq(trigger=Pin.IRQ_FALLING, handler=disminuir)
SET.irq(trigger=Pin.IRQ_FALLING, handler=cambiar)

# Variable global con la información
temperaturaideal = 23
temperatura = 0
flag = 1
adc = ADC(4)
tim = Timer()			# Temporizador

def temporizador(tim):	# Función de alarma
    global temperatura,flag,LED,temperaturaideal
    if (flag==1):
        val_adc = adc.read_u16()
        temperatura = float((100/65535)*val_adc)
        if temperatura == 100:
            temperatura = 99
        if (temperatura < temperaturaideal):
            LED.value(1)
            temperaturaideal = temperaturaideal - 0.5
        else:
            temperaturaideal = temperatura - 0.5
            LED.value(0) 
    else:
        LED.value(0)

# Inicializa al temporizador
tim.init(period=300, mode=Timer.PERIODIC, callback=temporizador)

# Los displays inician inhabilitados
T1.value(1)
T2.value(1)
T3.value(1)
T4.value(1)


while 1:
    funcion()
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT