from machine import Pin
from dht import DHT22
import time
sensor = DHT22(Pin(28))
ventilador = Pin(16, Pin.OUT) # LED simulando ventilador
segments = [
Pin(0, Pin.OUT), # A
Pin(1, Pin.OUT), # B
Pin(2, Pin.OUT), # C
Pin(3, Pin.OUT), # D
Pin(4, Pin.OUT), # E
Pin(5, Pin.OUT), # F
Pin(6, Pin.OUT) # G
]
digit1 = Pin(10, Pin.OUT) # Decenas
digit2 = Pin(11, Pin.OUT) # Unidades
numeros = {
0: [0,0,0,0,0,0,1],
1: [1,0,0,1,1,1,1],
2: [0,0,1,0,0,1,0],
3: [0,0,0,0,1,1,0],
4: [1,0,0,1,1,0,0],
5: [0,1,0,0,1,0,0],
6: [0,1,0,0,0,0,0],
7: [0,0,0,1,1,1,1],
8: [0,0,0,0,0,0,0],
9: [0,0,0,0,1,0,0]
}
def apagar_digitos():
digit1.value(0)
digit2.value(0)
def mostrar_numero(numero):
patron = numeros[numero]
for i in range(7):
segments[i].value(patron[i])
def multiplexar(valor):
decenas = valor // 10
unidades = valor % 10
apagar_digitos()
mostrar_numero(decenas)
digit1.value(1)
time.sleep_ms(4)
apagar_digitos()
mostrar_numero(unidades)
digit2.value(1)
time.sleep_ms(4)
apagar_digitos()
temperatura = 0
ultimo_tiempo = time.ticks_ms()
while True:
multiplexar(temperatura)
if time.ticks_diff(time.ticks_ms(), ultimo_tiempo) > 2000:
try:
sensor.measure()
temperatura = int(sensor.temperature())
if temperatura > 99:
temperatura = 99
if temperatura < 0:
temperatura = 0
if temperatura > 30:
ventilador.value(1)
else:
ventilador.value(0)
except:
pass
ultimo_tiempo = time.ticks_ms()