from machine import Pin
from time import sleep
from dht import DHT22

LED_TEMP = Pin(9, Pin.OUT)
LED_HUMI = Pin(12, Pin.OUT)
BUTTON = Pin(4, Pin.IN)
DHT = DHT22(Pin(18))
DHT.measure()

def take_dht():
  """return temperature and humidity value"""
  return DHT.temperature(), DHT.humidity()

def LED_control(temp, humi):
  """control LED blinking. set 0.5 seconds as one standard unit (ease for calculations)"""
  # set LED_TEMP blinking period about temperature
  if temp < 40:
    temp_T = 6
  elif temp < 60:
    temp_T = 3
  else:
    temp_T = 2
  
  # set LED_HUMI blinking period about humidity
  if humi < 20:
    temp_H = 3
  elif humi < 80:
    temp_H = 2
  else:
    temp_H = 1
    
  # control blinking
  for i in range(6):
    if i%temp_T == 0:
      LED_TEMP.on()
    if i%temp_H == 0:
      LED_HUMI.on()
    sleep(0.1)
    LED_TEMP.off()
    LED_HUMI.off()
    sleep(0.4)

while True:
  if BUTTON.value() == 1:
    temp, humi = take_dht()
    LED_control(temp, humi)


# reference
# https://wokwi.com/projects/382343978904776705 - DHT22 sensor
# https://wokwi.com/projects/357629785846167553 - button, LED
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT