DeaD_EyE

ESP32 Poti => LED Bar (WS2812)

Feb 25th, 2026
8,867
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import time
  2. from machine import ADC, Pin, idle
  3. from neopixel import NeoPixel
  4.  
  5. LEDS = 20
  6. OFFSET = 1449
  7.  
  8. adc0 = ADC(Pin(10))
  9. np = NeoPixel(Pin(15), LEDS)
  10. np.fill((0,0,0))
  11. np.write()
  12.  
  13.  
  14. class IIRLowPassShift:
  15.     def __init__(self, shift: int, initial: int=0, offset: int=0):
  16.         self.shift = shift
  17.         self.y = initial
  18.         self.offset = offset
  19.  
  20.     def update(self):
  21.         x = adc0.read_u16()
  22.         self.y = self.y + ((x - self.y) >> self.shift)
  23.         return max(0, self.y - self.offset)
  24.  
  25.  
  26. low_pass = IIRLowPassShift(4, offset=OFFSET)
  27.  
  28.  
  29. while True:
  30.     value = low_pass.update() / 65536 * LEDS
  31.     value, fraction = divmod(value, 1)
  32.     # print(value, fraction)
  33.     for i in range(LEDS):
  34.         if value > i:
  35.             np[i] = (255, 0, 0)
  36.         elif fraction:
  37.             np[i] = (int(fraction * 255), 0, 0)
  38.             fraction = 0
  39.         else:    
  40.             np[i] = (0, 0, 0)
  41.        
  42.     np.write()
  43.     # time.sleep_ms(1)
  44.     idle()
  45.  
Advertisement