''' 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,brightness) 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 time.sleep(5) # wait 5 sec. before start while True: sound = vu_meter() average = average/50*49 + sound/50 softgain = 4/average # selfcalibrating if sound < 0.03: #mute - depending on the microphone and ambient noise 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.