import machine, neopixel, time
from machine import ADC
leds = 25
np = neopixel.NeoPixel(machine.Pin(2), leds) # Pin 4 as data pin
adc = ADC(machine.Pin(34)) # Analog read from Pin 22

def read_sound_level():
    return adc.read() # Reads analog value from 0-4095 on ESP32

def map_sound_to_color(sound_level):
    intensity = min(255, int(sound_level / 16)) # Map the 12-bit value to an 8-bit value
    return (intensity, 0, 0) # Only use red color

def fade_pixel(np, index):
    r, _, _ = np[index]
    np[index] = (int(r*0.9), 0, 0) # Only fade red component

while True:
    sound_level = read_sound_level()
    new_color = map_sound_to_color(sound_level)
    
    # Shift and fade colors
    for i in range(leds - 1, 0, -1):
        np[i] = np[i-1]
    np[0] = new_color

    for i in range(1, leds):
        fade_pixel(np, i)

    np.write()
    time.sleep_ms(33) # Control speed to aim for ~30 FPS