import dht
import  neopixel
from machine import Pin, ADC 
from time import sleep 
from random import randrange as r

def convert(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min)

button1 = Pin(27, Pin.IN, Pin.PULL_UP)
button2 = Pin(26, Pin.IN, Pin.PULL_UP)
nl = 160
leds = neopixel.NeoPixel(Pin(13), nl)
d=0.007
pot1 = ADC(Pin(14))
pressed1 = True
pressed2 = True
sense = dht.DHT22(Pin(4))

while True:
  col3 = [r(100,255),r(100,255),r(100,255)]
  col4 = [r(100,255),r(100,255),r(100,255)]
  if not button1.value():
    if pressed1:
      pressed1 = False
      for l in range(nl):
        sense.measure()
        print(sense.humidity(), sense.temperature())
        h = convert(sense.humidity(), 0,100,0,10)
        t = convert(sense.temperature(),0,80,0,10)
        spanje1 = (pot1.read())/10000
        leds[l]= ([25*int(t),0,0])
        sleep(spanje1)
        if l>=64:
          leds[l]= ([0,0,25*int(h)])
          sleep(d*6)
        if l>=112:
          leds[l]=col3
          sleep(d*9)
        if l>=144:
          leds[l]=col4
          sleep(d*12)
        leds.write()
        if not button2.value():
          if pressed2:
            pressed2 = False
            leds.fill([0,0,0])
            
        else:
          pressed2=True
               
  else:
    pressed1=True