简体   繁体   中英

Changing neopixels randomly between 3 colors, on each pixel randomly

So I currently have 2x 12 Pixel Neopixel rings, running from a pi zero W.

The LEDs all work as expected, going through level shifter etc. and all react as expected.

Total noob with python but I can control the pixels and get them to do what I want, however I'm currently wanting to have each pixel on each ring randomly switch between 3 set colors at random times. Basically to have a flickering effect but only in that color range.

Currently I'm just changing each pixel manually in a function, that gets called from my script, that loops for a certain amount of time. It works ok but is a little inelegant.

def Lights_Flicker():
    
    pixels[0] = (255, 0, 0,0)
    pixels.show()
    time.sleep(.02)
    
    pixels[12] = (255, 0, 0,0)
    pixels.show()
    time.sleep(.02)

    pixels[6] = (122, 100, 0,0)
    pixels.show()
    time.sleep(.02)
    
    pixels[23] = (122, 100, 0,0)
    pixels.show()
    time.sleep(.02)

Using this to loop through the function for x seconds. (While Sound is playing)

while time.time() < t_end:
            Lights_Flicker()

I'm happy with the timing portion its just the color flickering. If someone knows how to do this a little more cleanly that would be amazing.

Thanks for looking

I believe something like this should do what you're asking...

from random import randint

colors = [ (255,0,0,0), # put all color tuples here
           (122,100,0,0),
         ]

pixel_num = [ 0, # put all pixels here
              6,
              12,
              23,
            ]

while time.time() <= t_end:
    rand_pixel = randint(0, len(pixel_num)-1)
    rand_color = randint(0, len(colors)-1)

    new_pixel = pixel_num[rand_pixel]
    new_color = colors[rand_color]

    pixels[new_pixel] = new_color
    pixels.show()
    
    time.sleep(0.02)
    
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM