簡體   English   中英

在 3 種顏色之間隨機更改新像素,在每個像素上隨機更改

[英]Changing neopixels randomly between 3 colors, on each pixel randomly

所以我目前有 2 個 12 像素 Neopixel 環,從 pi 零 W 運行。

LED 都按預期工作,經過電平轉換器等,並按預期做出反應。

使用 python 的總菜鳥,但我可以控制像素並讓它們做我想做的事情,但是我目前希望每個環上的每個像素隨機在 3 組顏色之間隨機切換。 基本上具有閃爍效果,但僅限於該顏色范圍。

目前我只是在一個函數中手動更改每個像素,該函數從我的腳本中調用,循環一定時間。 它工作正常,但有點不優雅。

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)

使用它來循環函數 x 秒。 (播放聲音時)

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

我對計時部分很滿意,只是顏色閃爍。 如果有人知道如何更干凈地做到這一點,那就太棒了。

感謝您的關注

我相信這樣的事情應該做你所要求的......

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)
    
    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM