Format sound reactive neopixel peace pendant
MODIFY
Sound_Reactive_NeoPixel_Peace_Pendant/Sound_Reactive_NeoPixel_Peace_Pendant.py
Sound_Reactive_NeoPixel_Peace_Pendant/Sound_Reactive_NeoPixel_Peace_Pendant.py
+import array
+
import board
import neopixel
-import time
from analogio import AnalogIn
-import array
led_pin = board.D0 # NeoPixel LED strand is connected to GPIO #0 / D0
n_pixels = 12 # Number of pixels you are using
@@ -12,12 +12,12 @@ samples = 60 # Length of buffer for dynamic level adjustment
top = n_pixels + 1 # Allow dot to go slightly off scale
peak = 0 # Used for falling dot
-dotcount = 0 # Frame counter for delaying dot-falling speed
-volcount = 0 # Frame counter for storing past volume data
+dot_count = 0 # Frame counter for delaying dot-falling speed
+vol_count = 0 # Frame counter for storing past volume data
lvl = 10 # Current "dampened" audio level
-minlvlavg = 0 # For dynamic adjustment of graph low & high
-maxlvlavg = 512
+min_level_avg = 0 # For dynamic adjustment of graph low & high
+max_level_avg = 512
# Collection of prior volume samples
vol = array.array('H', [0] * samples)
@@ -26,21 +26,23 @@ mic_pin = AnalogIn(board.A1)
strip = neopixel.NeoPixel(led_pin, n_pixels, brightness=.1, auto_write=True)
+
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255):
return (0, 0, 0)
- if (pos < 85):
+ if pos < 85:
return (int(pos * 3), int(255 - (pos * 3)), 0)
- elif (pos < 170):
+ elif pos < 170:
pos -= 85
return (int(255 - pos * 3), 0, int(pos * 3))
else:
pos -= 170
return (0, int(pos * 3), int(255 - pos * 3))
-def remapRange(value, leftMin, leftMax, rightMin, rightMax):
+
+def remap_range(value, leftMin, leftMax, rightMin, rightMax):
# this remaps a value from original (left) range to new (right) range
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
@@ -52,51 +54,54 @@ def remapRange(value, leftMin, leftMax, rightMin, rightMax):
# Convert the 0-1 range into a value in the right range.
return int(rightMin + (valueScaled * rightSpan))
+
while True:
n = int((mic_pin.value / 65536) * 1000) # 10-bit ADC format
n = abs(n - 512 - dc_offset) # Center on zero
- if ( n >= noise ): # Remove noise/hum
+ if n >= noise: # Remove noise/hum
n = n - noise
lvl = int(((lvl * 7) + n) / 8) # "Dampened" reading (else looks twitchy) - divide by 8 (2^3)
# Calculate bar height based on dynamic min/max levels (fixed point):
- height = top * (lvl - minlvlavg) / (maxlvlavg - minlvlavg)
+ height = top * (lvl - min_level_avg) / (max_level_avg - min_level_avg)
# Clip output
- if(height < 0):
+ if height < 0:
height = 0
- elif (height > top):
+ elif height > top:
height = top
# Keep 'peak' dot at top
- if(height > peak):
+ if height > peak:
peak = height
# Color pixels based on rainbow gradient
for i in range(0, len(strip)):
- if (i >= height):
+ if i >= height:
strip[i] = [0, 0, 0]
else:
- strip[i] = wheel(remapRange(i, 0, (n_pixels - 1), 30, 150))
+ strip[i] = wheel(remap_range(i, 0, (n_pixels - 1), 30, 150))
# Save sample for dynamic leveling
- vol[volcount] = n
+ vol[vol_count] = n
# Advance/rollover sample counter
- if (++volcount >= samples):
- volcount = 0
+ vol_count += 1
+
+ if vol_count >= samples:
+ vol_count = 0
# Get volume range of prior frames
- minlvl = vol[0]
- maxlvl = vol[0]
+ min_level = vol[0]
+ max_level = vol[0]
for i in range(1, len(vol)):
- if(vol[i] < minlvl):
- minlvl = vol[i]
- elif (vol[i] > maxlvl):
- maxlvl = vol[i]
+ if vol[i] < min_level:
+ min_level = vol[i]
+ elif vol[i] > max_level:
+ max_level = vol[i]
# minlvl and maxlvl indicate the volume range over prior frames, used
# for vertically scaling the output graph (so it looks interesting
@@ -104,11 +109,10 @@ while True:
# (e.g. at very low volume levels) the graph becomes super coarse
# and 'jumpy'...so keep some minimum distance between them (this
# also lets the graph go to zero when no sound is playing):
- if( (maxlvl - minlvl) < top ):
- maxlvl = minlvl + top
+ if (max_level - min_level) < top:
+ max_level = min_level + top
- minlvlavg = ( minlvlavg * 63 + minlvl ) >> 6 # Dampen min/max levels - divide by 64 (2^6)
- maxlvlavg = ( maxlvlavg * 63 + maxlvl ) >> 6 # fake rolling average - divide by 64 (2^6)
+ min_level_avg = (min_level_avg * 63 + min_level) >> 6 # Dampen min/max levels - divide by 64 (2^6)
+ max_level_avg = (max_level_avg * 63 + max_level) >> 6 # fake rolling average - divide by 64 (2^6)
print(n)
-

Format sound reactive neopixel peace pendant