from machine import Pin, ADC
import neopixel
from utime import sleep, ticks_ms

NUM_LED = 16
leds = neopixel.NeoPixel(Pin(16), NUM_LED)
pot = ADC(Pin(32))
pot.atten(ADC.ATTN_11DB)
gumb = Pin(14, Pin.IN, Pin.PULL_UP)


led = Pin(15, Pin.OUT)


button = Pin(4, Pin.IN, Pin.PULL_UP)

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

while True:
    if gumb.value() == 0:  
        leds.fill([0, 0, 0])  
        leds.write()
    else:
        leds.fill([0, 0, 0])
        g = 255
        z = round(g / NUM_LED)

        for x in range(NUM_LED):
            pot_value = pot.read()
            s = convert(pot_value, 0, 4096, 5, 500) * 0.001  
            leds[x] = [g, g, g]  
            leds.write()
            sleep(s) 
            g -= z  

    
    led.value(1)  
    start_time = ticks_ms()  

    while button.value() == 1:  
        pass

    end_time = ticks_ms()  
    elapsed_time = end_time - start_time  
    print(f"Elapsed time: {elapsed_time} milliseconds")

    led.value(0)  
    sleep(2)