Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'''
Example program for use in the soldering workshop.
Should work on a WS2812 strip of any length.
The variable "softgain" controls the sensitivity in selfcalibrating mode and
the length of the strip is entered in "number_pixels".
Works with a MAX4466 microphone amplifier module.
Version: 0.2 from 26.05.2024
License: CC-BY Weja/HoFaLab
'''
number_pixels = 10
brightness = 0.4 # value between 0 and 1
import WS2812onRP2040
import time
from machine import ADC, Pin, Timer
softgain,mode,average = 0,0,0 #init values
mic = ADC(Pin(28)) # create ADC object on ADC pin
pixels = WS2812onRP2040.strip(number_pixels,29,0.3)
timer1 = Timer(period=10000, mode=Timer.PERIODIC, callback=lambda t:changemode())
def changemode(): #0-left,1-right,2-center lights
global mode
mode = (mode+1)%3
def vu_meter():
vu=0
for r in range(500):
vu+=abs(mic.read_u16() - 32767)
return (vu/500)/32767
while True:
sound = vu_meter()
average = average/50*49 + sound/50
softgain = 4/average # selfcalibrating
if sound < 0.02: #mute
softgain=1
hue = min(int(sound*softgain*50),359)
#print(sound,softgain,hue) #debug info
col = WS2812onRP2040.hue2col(hue)
anz = min(int(sound*softgain),number_pixels)
pixels.fill((0,0,0))
for p in range(anz):
if mode == 0: #left to right
pixels.pset(number_pixels-1-p,(col))
if mode == 1: #right to left
pixels.pset(p,(col))
if mode == 2: # left/right to center
pixels.pset(p,(col))
pixels.pset(number_pixels-1-p,(col))
pixels.show()
time.sleep(0.125) # refresh time for the lights.