Python Forum
Need help with code for my WS2812B (Neopixel) Led Strip
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with code for my WS2812B (Neopixel) Led Strip
#1
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. Dance

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.
[Image: NIkGRUu.png]

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
[Image: VLeDp6I.gif]

щ(ಥДಥщ)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()
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 2,790 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,151 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Can't strip extra characters from Data Canflyguy 7 1,813 Jan-10-2022, 02:16 PM
Last Post: Canflyguy
  strip() pprod 8 3,373 Feb-16-2021, 01:11 PM
Last Post: buran
  split strip issues 'NonType' casacafe 8 3,762 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  removing spaces/tabs after used .strip() zarize 0 1,554 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 5,226 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  strip space from end of a row of text ineuw 4 2,823 Apr-15-2019, 03:14 AM
Last Post: ineuw
  How to remove whitespace from a string when .replace and .strip do not work winnetrie 7 4,372 Jan-05-2019, 08:44 AM
Last Post: DeaD_EyE
  Strip does not work when applied on a string read from a text file. susmis666 1 2,336 Dec-27-2018, 07:07 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020