from machine import Pin, ADC, Timer
from time import sleep
import time
from math import log
analog = ADC(Pin(26))
button = Pin(15, Pin.IN, Pin.PULL_DOWN)
led = Pin(2, Pin.OUT)
button_toggled = False
def interrupt_callback(pin):
global button_toggled
stamp = 2
pressed_time = time.ticks_ms() - stamp
if pressed_time >= 199.99:
button_toggled = True
stamp = pressed_time
def toggle(pin):
led.toggle()
button.irq(trigger=Pin.IRQ_RISING, handler=interrupt_callback)
timer = Timer(period=1000, mode=Timer.PERIODIC, callback=toggle)
while True:
if button_toggled == True:
BETA = 3950
value = analog.read_u16()
celsius = round(1 / (log(1 / (65535. / value - 1)) / BETA + 1.0 / 298.15) - 273.15)
print(f"{celsius}°C")
button_toggled = False
if button_toggled == False:
sleep(0.5)