顯示具有 NeoPixel 標籤的文章。 顯示所有文章
顯示具有 NeoPixel 標籤的文章。 顯示所有文章

2024年7月25日 星期四

使用sprkfun的micro:bit底座測試neopixel WS2812b燈控的功能

sprkfun的micro:bit底座的資料:micro:bit Breakout Board Hookup Guide

micro:bit版本:V2

MakeCode測試:

擴展套件:Adafruit Neopixel Driver

積木程式:


修改上圖 Pin腳位,測試結果Pin 0, 1, 2, 5, 8, 9, 11, 12, 13, 14, 15, 16可以使用neopixel。

改採Python Editor:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from microbit import *
import neopixel
from random import randint

# Setup the Neopixel strip on pin0 with a length of 8 pixels
np = neopixel.NeoPixel(pin0, 16)
np.clear()

while True:
    #Iterate over each LED in the strip

    for pixel_id in range(0, len(np)):
        red = randint(0, 255)
        green = randint(0, 255)
        blue = randint(0, 255)

        # Assign the current LED a random red, green and blue value between 0 and 60
        np[pixel_id] = (red, green, blue)

        # Display the current pixel data on the Neopixel strip
        np.show()
    sleep(300)

測試結果Pin 0~16都可以使用neopixel,但Pin 3, 4,6, 7, 10會使5x5 矩陣LED,造成不正常顯示,其主要原因是這幾支腳位做為LED Col使用。




2024年1月28日 星期日

micro:bit -悟空板(WuKong):NeoPixel測試 (Python版)

範例一:分別以紅色、綠色、藍色、白色點亮悟空板上的四顆燈 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Imports go at the top
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin16, 4)


# Code in a 'while True:' loop repeats forever
while True:
    np[0] = (255, 0, 0)
    np[1] = (0, 255, 0)
    np[2] = (0, 0, 255)
    np[3] = (255, 255, 255)
    np.show()

執行結果:

範例二:同範例一,顯示資料用串列變數來儲存

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Imports go at the top
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin16, 4)

leds=[(255,0,0), (0,255,0), (0,0,255), (255,255,255)]
# Code in a 'while True:' loop repeats forever
while True:
    np[0] = leds[0]
    np[1] = leds[1]
    np[2] = leds[2]
    np[3] = leds[3]
    np.show()

執行結果同上題

範列三:每隔0.5秒燈色旋轉

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Imports go at the top
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin16, 4)
i = 0
leds=[(255,0,0), (0,255,0), (0,0,255), (255,255,255)]
# Code in a 'while True:' loop repeats forever
while True:
    np[0] = leds[i]
    np[1] = leds[(i+1)%4]
    np[2] = leds[(i+2)%4]
    np[3] = leds[(i+3)%4]
    np.show()
    sleep(500)
    i=(i+1)%4

執行結果:


範例四、用迴圈改寫程式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Imports go at the top
from microbit import *
import neopixel
np = neopixel.NeoPixel(pin16, 4)
i = 0
leds=[(255,0,0), (0,255,0), (0,0,255), (255,255,255)]
# Code in a 'while True:' loop repeats forever
while True:
    for j in range(4):
        np[j] = leds[(i+j)%4]
    np.show()
    sleep(500)
    i=(i+1)%4

執行結果:
同範例三