diff --git a/main.py b/main.py index 29a47e5..54f6ecb 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,14 @@ board.IO21, board.IO26, # type: ignore board.IO47, + board.IO33, # type: ignore + board.IO34, # type: ignore + board.IO48, + board.IO35, + board.IO36, + board.IO37, + board.IO38, + board.IO39 # do the rest... ] @@ -21,16 +29,48 @@ for led in leds: led.direction = Direction.OUTPUT -# main loop -while True: - volume = microphone.value +# Set a new max volume for normalization +max_volume_value = 42000 # Adjust based on observed sensitivity +decay_rate = 0.05 # how fast or slow the volume goes down + +# Track previous displayed volume +displayed_volume = 0 + +# Function to normalize and filter microphone volume level +def get_volume(): + # Read raw microphone value + raw_value = microphone.value + + # divide the raw value measured by the max volume set to get the normalized value between 0 and 1 + normalized_value = raw_value / max_volume_value + + # Set a threshold to filter out low noise values + volume = max(0, normalized_value - 0.1) # Adjust threshold if necessary + + return volume - print(volume) +# Function to update LEDs based on the displayed volume level +def update_leds(volume): + led_count = len(leds) + led_to_turn_on = int(volume * led_count) + + # Turn on LEDs up to led_to_turn_on and turn off the rest + for i in range(led_count): + leds[i].value = i < led_to_turn_on + +# Main loop +while True: + volume = get_volume() # Get the current volume level + print(f"Current volume): {volume}") # Print the volume for monitoring - leds[0].value = not leds[0].value - leds[1].value = not leds[0].value + # Fast rise, slow decay logic + if volume > displayed_volume: + displayed_volume = volume # Immediate update if volume is higher + else: + displayed_volume = max(0, displayed_volume - decay_rate) # Gradual decay - sleep(1) + update_leds(displayed_volume) # Update LEDs based on displayed volume + sleep(0.05) # Small delay for smooth updates # instead of blinking, # how can you make the LEDs