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

nl = 46
nl2 = 30
nl3 = 16
potPin = 25
leds = neopixel.NeoPixel(Pin(19), 46)
pot = ADC(Pin(potPin))
button = Pin(2, Pin.IN, Pin.PULL_UP)
sense = dht.DHT22(Pin(4))

led2 = neopixel.NeoPixel(Pin(21), 16)

NUM_LED = 50
potPin = 14
leds1 = neopixel.NeoPixel(Pin(27), NUM_LED)
pot = ADC(Pin(potPin))
st = 0

button1 = Pin(17, Pin.IN, Pin.PULL_UP)

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

Steps = 255//nl
Steps2 = 255//nl2
x = 0

print("Nastavite hitrost na potenciometru in kliknite sivi gumb za led animacijo ali kliknite rdeči gumb za igro s potenciometrom!")

a = random.randint(1, 50)

start1 = time.time()
finish1 = time.time()

while True:
  if button.value() == 0 or button1.value() == 0:
    start1 = time.time()
  while (finish1 - start1) < 60:
    
    for x in range(16):
      led2.fill([random.randint(0,255),random.randint(0,255),random.randint(0,255)])
      led2.write()
      sleep(0.5)
      led2.fill([0,0,0])
      led2.write()
      sleep(0.5)


    if button.value() == 0:
      hitorst1 = convert(pot.read(), 0, 4096,0, nl2)
      hitorst2 = convert(pot.read(), 0, 4096,0, nl3)
      if hitorst1 <= 14:
        h1 = 0.05
      elif hitorst1 >= 15:
        h1 = 0.009
      if hitorst2 <= 14:
        h2 = 0.05
      elif hitorst2 >= 15:
        h2 = 0.009
      if x % 2 == 0:
            for i in range(nl2):
                leds[i] = [255 - (i*Steps), i*Steps, 0]
                time.sleep(h1)
                leds.write()
            for i in range(nl2,nl):
                leds[i] = [i*Steps2, 255 - (i*Steps2), 0]
                time.sleep(h2)
                leds.write()
      else:
          for i in range(nl2,nl):
              leds[i] = [255 - (i*Steps2), i*Steps2, 0]
              time.sleep(h2)
              leds.write()
          for i in range(nl2):
              leds[i] = [i*Steps, 255 - (i*Steps), 0]
              time.sleep(h1)
              leds.write()
      leds.fill([0,0,0])
      leds.write()
      x += 1
    elif button1.value() == 0:
      start = time.time()
      for i in range(5):
        a = random.randint(1, 50)
        leds1.fill([0,0,0])
        leds1[a] = [0,250,0]
        leds1.write()
        poten = convert(pot.read(),0,4096,0,NUM_LED)
        time1 = 0
        time2 = 0
        while (time2-time1) <= 0.5:
          time1 = time.time()
          st += 1
          a = random.randint(1, 50)
          while poten != a:
            leds1[poten] = [250,0,0]
            leds1.write()
            poten = convert(pot.read(),0,4096,0,NUM_LED)
            leds1.fill([0,0,0])
            leds1[a] = [0,250,0]
            leds1.write()
          time2 = time.time()
      finish = time.time()
      print("Čas: ", end ="")
      print(finish-start,end="")
      print("s")
      print("Število zadetih lučk:", end=" ")
      print(st)
      print("Nastavite hitrost na potenciometru in kliknite sivi gumb za led animacijo ali kliknite rdeči gumb za igro s potenciometrom!")
    finish1 = time.time()