import dht
import neopixel
from machine import Pin, ADC 
from utime import sleep 
import random

nl = 32
leds = neopixel.NeoPixel(Pin(15), nl)
r = 0
g = 0
b = 255
c1 = True

btnGreen = Pin(2, Pin.IN, Pin.PULL_UP)
start = False

sense = dht.DHT22(Pin(13))
def convert(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
for c in range(1):
    print(c, convert(c, 0, 4096, 0, 16))

while True:
    btnStateGreen = not bool(btnGreen.value())
    if btnStateGreen == True:
        start = True
    if start == True:
        leds.fill([0,0,0])
        leds.write()
        for i in range(32):
            sense.measure()
            hum = sense.humidity()
            temp=sense.temperature()
            #print(f" Vlaga: {sense.humidity()}%, Temperatura: {sense.temperature()}°C")
            h = convert(hum, -80,100,0,10)
            t = convert(temp,-100,80,0,10)
            speed = t /20

            print (speed, r,g,b)

            leds[i] =([r,g,b])
            if i % 2 == 0:
                leds[-i] = ([0,255,255])
            else:
                leds[-i] = ([255,0,150])
            
            if r < 200:
                r = r + 11

            if r > 200:
                r = 225
                b = b - 11

            if b < 10:
                    b = 255
                    r = 0

            if i == 0:
                leds[0] = ([r,g,b])

            leds.write()
            sleep(speed)

            leds.fill([0,0,0])
            leds.write()
            sleep(speed)
        
        b = 255
        r = 0

        sleep(speed)
        leds[0] = ([0,255,255])

        leds.write()
        sleep(speed)

        leds.fill([0,0,0])
        leds.write()
        sleep(speed)