DeaD_EyE icon

ESP32 Poti => LED Bar (WS2812)

DeaD_EyE | PRO | 02/25/26 11:11:45 AM UTC | 0 ⭐ | 12853 👁️ | Never ⏰ | []
Python |

980 B

|

None

|

0 👍

/

0 👎

import time
from machine import ADC, Pin, idle
from neopixel import NeoPixel
 
LEDS = 20
OFFSET = 1449
 
adc0 = ADC(Pin(10))
np = NeoPixel(Pin(15), LEDS)
np.fill((0,0,0))
np.write()
 
 
class IIRLowPassShift:
    def __init__(self, shift: int, initial: int=0, offset: int=0):
        self.shift = shift
        self.y = initial
        self.offset = offset
 
    def update(self):
        x = adc0.read_u16()
        self.y = self.y + ((x - self.y) >> self.shift)
        return max(0, self.y - self.offset)
 
 
low_pass = IIRLowPassShift(4, offset=OFFSET)
 
 
while True:
    value = low_pass.update() / 65536 * LEDS
    value, fraction = divmod(value, 1)
    # print(value, fraction)
    for i in range(LEDS):
        if value > i:
            np[i] = (255, 0, 0)
        elif fraction:
            np[i] = (int(fraction * 255), 0, 0)
            fraction = 0
        else:    
            np[i] = (0, 0, 0)
        
    np.write()
    # time.sleep_ms(1)
    idle()
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎