from machine import Pin, ADC, PWM
from time import sleep
from math import log
ntc = ADC(Pin(14))
ntc.atten(ADC.ATTN_11DB)
ledbargraph = [
Pin(23, Pin.OUT),
Pin(22, Pin.OUT),
Pin(21, Pin.OUT),
Pin(19, Pin.OUT),
Pin(18, Pin.OUT),
Pin(5, Pin.OUT),
Pin(17, Pin.OUT),
Pin(16, Pin.OUT),
Pin(4, Pin.OUT),
Pin(15, Pin.OUT),
]
leds_to_on = 0
temperature_total_value = 0
mapped_temperature = 0
buzzer = PWM(Pin(2, Pin.OUT))
buzzer.freq(400)
buzzer.duty(0)
LEDBARGRAPH_SIZE = 10
MAX_TEMPERATURE = 80 # Celsius
MIN_TEMPERATURE = -24 # Celsius
TOTAL_VALUES = abs(MAX_TEMPERATURE) + abs(MIN_TEMPERATURE) + 1
TEMPERATURE_TO_EACH_LED = TOTAL_VALUES // LEDBARGRAPH_SIZE
def get_temperature_celsius():
BETA = 3950
analog_value = ntc.read()
celsius = 1 / (log(1 / (4096 / analog_value - 1)) / BETA + 1.0 / 298.15) - 273.15
return celsius
def turn_off_leds(quantity_of_leds: int):
for led in ledbargraph:
led.off()
def turn_on_leds(quantity_of_leds: int):
if quantity_of_leds <= 0:
quantity_of_leds = 1
print(f"Leds para acender: {quantity_of_leds}")
for i in range(quantity_of_leds):
ledbargraph[i].on()
def alert_buzzer():
if mapped_temperature < 90:
buzzer.duty(0)
return
buzzer.duty(1)
try:
while True:
temperature = get_temperature_celsius()
if temperature <= 0:
mapped_temperature = abs(abs(temperature) - abs(MIN_TEMPERATURE))
else:
mapped_temperature = temperature + 25
mapped_temperature = round(mapped_temperature)
leds_to_on = mapped_temperature // TEMPERATURE_TO_EACH_LED
turn_off_leds(LEDBARGRAPH_SIZE - leds_to_on)
turn_on_leds(leds_to_on)
print(f"Temperatura: {temperature}")
alert_buzzer()
except KeyboardInterrupt:
print('Programa finalizado')