Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AKalakota23 authored Oct 17, 2024
1 parent a560142 commit 25ee6cf
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
]

Expand All @@ -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
Expand Down

0 comments on commit 25ee6cf

Please sign in to comment.