from time import sleep
from machine import Pin as pin, ADC
sleep(0.1) # Wait for USB to become ready
# pin(23, pin.OUT).value(1)
pot = ADC(pin(26))
fan = pin(15, pin.OUT)
def map(x, in_min, in_max, out_min, out_max): #Maps the analog readings into a integer range of our wishing
return int((x-in_min) * (out_max-out_min) / (in_max - in_min) + out_min)
def temp_value(adc): #Gets readings from potentiometer and sets the range to [5;30]
analog_value = adc.read_u16()
temp_read = map(analog_value, 0, 65535, 10, 80)
sleep(0.3)
return temp_read
while True:
temp = temp_value(pot)
print(temp)
if temp >= 40:
fan.value(1)
else:
fan.value(0)