from machine import Pin,ADC
from time import sleep,sleep_ms
import neopixel
import math
relay=Pin(14,Pin.OUT)
BETA = 3950
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
np=neopixel.NeoPixel(Pin(4),16)
def read_ntc_temperature():
analog_value = adc.read()
print(analog_value)
temperature_celsius = 1 / (math.log(1 / (4096 / analog_value - 1)) / BETA + 1.0 / 298.15) - 273.15
return temperature_celsius
relay.value(1)
import time
def demo(np):
n = np.n
# cycle
for i in range(4 * n):
for j in range(n):
np[j] = (0, 0, 0)
np[i % n] = (255, 255, 255)
np.write()
sleep_ms(25)
# bounce
for i in range(4 * n):
for j in range(n):
np[j] = (0, 0, 128)
if (i // n) % 2 == 0:
np[i % n] = (0, 0, 0)
else:
np[n - 1 - (i % n)] = (0, 0, 0)
np.write()
sleep_ms(60)
# fade in/out
for i in range(0, 4 * 256, 8):
for j in range(n):
if (i // 256) % 2 == 0:
val = i & 0xff
else:
val = 255 - (i & 0xff)
np[j] = (val, 0, 0)
np.write()
# clear
for i in range(n):
np[i] = (0, 0, 0)
np.write()
while True:
temperature = read_ntc_temperature()
print("Temperature: {:.2f} ℃".format(temperature))
if temperature>27:
relay.value(1)
if temperature<=27 and temperature>=24:
relay.value(1)
if temperature<24:
relay.value(0)
sleep(1)
demo(np)