Need help with code for my WS2812B (Neopixel) Led Strip - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help with code for my WS2812B (Neopixel) Led Strip (/thread-25698.html) |
Need help with code for my WS2812B (Neopixel) Led Strip - Phibbl - Apr-08-2020 Hello everyone I just got myself a W2812B Led Strip for a personal project and hooked it up to a Raspberry Pi Zero. After some hours of fiddeling i finally got them to work and now they can display a solid color, yay. I'm using the "rpi_ws281x" package and code in IDLE. My coding skills are very limited though so that's what i came up with by following a tutorial. I want a fairly easy effect to run on my Led Strip, but i'm not really capable of programming it: 1. All Led's glow with 70% brightness 2. One after another lights up with its max brightness until the end of the strip is reached. 3. short pause I even animated it xD щ(ಥДಥщ)Im pretty shure that the code for this is quite easy but i just cant get it to work. pls help thanks in advance, Philipp Here's the code in written form: from rpi_ws281x import * # LED strip configuration: LED_COUNT = 300 # Number of LED pixels. LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!). #LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0). LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) LED_DMA = 10 # DMA channel to use for generating signal (try 10) LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53 strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ,LED_DMA,LED_INVERT,LED_BRIGHTNESS,LED_CHANNEL) strip.begin() for x in range(0,LED_COUNT): strip.setPixelColor(x,Color(255,0,0)) strip.show() RE: Need help with code for my WS2812B (Neopixel) Led Strip - deanhystad - Apr-08-2020 You could build up quite a library of different effects. I wrote a couple of chase effects, one that chases a single lamp across the strand and another that does a gradient kind of thing. I used turtle graphics for testing, but I think you are more interested in ideas on how to proceed than actual code. import time import turtle def Color(r,g,b): return(r/255, g/255, b/255) OFF = Color(0, 0, 0) LED_COUNT = 40 SCREEN_WIDE = 450 class LEDStrip: def __init__(self): self.screen = turtle.Screen() self.screen.setup(SCREEN_WIDE, 100) self.screen.tracer(0) self.pen = turtle.Turtle(shape='circle', visible=False) self.pen.shapesize(0.5, 0.5) def setPixelColor(self, lamp, color): self.pen.penup() self.pen.goto(lamp*10 - 200, 0) self.pen.color(color) self.pen.stamp() def update(self): turtle.update() strip = LEDStrip() def light_one_lamp(lamp, on, off=OFF): """Sets lamp to on, all other lamps to off""" for x in range(LED_COUNT): if x == lamp: strip.setPixelColor(x, on) else: strip.setPixelColor(x, off) strip.update() def gradient(lamp, on): """Set one lamp to on. Set adjacent lamps to dimmer version of on""" for x in range(LED_COUNT): # Dim lamps futher away from lamp, Exp used to adjust dimming brightness = ((LED_COUNT - abs(lamp - x)) / LED_COUNT)**4 c = [rgb*brightness for rgb in on] strip.setPixelColor(x, c) strip.update() def chase_single(on, off=OFF, start=0, stop=LED_COUNT, incr=1, sleeptime=0.05): """Chase a single bulb from start to stop""" for lamp in range(start, stop, incr): light_one_lamp(lamp, on, off) # time.sleep(sleeptime) def chase_gradient(on, start=0, stop=LED_COUNT, incr=1, sleeptime=0.05): """Chase a single bulbe from start to stop. Surrounding bulbs are dimmed""" for lamp in range(start, stop, incr): gradient(lamp, on) # time.sleep(sleeptime) for i in range(4): incr = 2*i+1 chase_gradient(Color(0, 255, 0), incr=incr) for i in range(4): incr = 2*(i+1) chase_gradient(Color(255, 0, 0), incr=incr) chase_gradient(Color(255, 0, 0), LED_COUNT, 0, -incr) chase_single(Color(255, 0, 0), Color(0, 0, 255)) chase_single(Color(255, 0, 0), Color(0, 0, 254), LED_COUNT, 0, -1) |