溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MicroPython neopixle怎么用

發(fā)布時(shí)間:2022-01-13 14:31:24 來源:億速云 閱讀:907 作者:iii 欄目:互聯(lián)網(wǎng)科技

本文小編為大家詳細(xì)介紹“MicroPython neopixle怎么用”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“MicroPython neopixle怎么用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

microbit/newbit的MicroPython固件中,內(nèi)置了neopixel彩燈的控制,我們可以使用任意一個(gè)GPIO去控制neopixel,支持任意數(shù)量的彩燈。

import neopixel

np = neopixel.NeoPixel(pin1, 8)
np[0] = (0, 0, 200)
np[1] = (0, 50, 100)
np[2] = (200, 0, 0)
np.show()

函數(shù) neopixel.NeoPixel(PIN, NUM) 用來創(chuàng)建 neopixel 對象,它有兩個(gè)參數(shù),第一個(gè)是GPIO,第二個(gè)是彩燈的數(shù)量。

neopixel 對象是一個(gè)元組列表,每個(gè)列表項(xiàng)都是由 RGB 三種顏色組成的元組。RGB參數(shù)的范圍是 0-255,三種顏色組合起來就有 256 x 256 x 256 = 1.67M種顏色。

顏色參數(shù)寫入列表后并不能改變彩燈,還需要調(diào)用函數(shù) show(),才會(huì)更新。如果要清除彩燈,可以調(diào)用函數(shù) clear().

官方的例子,隨機(jī)顯示彩燈。

"""
    neopixel_random.py

    Repeatedly displays random colours onto the LED strip.
    This example requires a strip of 8 Neopixels (WS2812) connected to pin0.

"""
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, 8)

while True:
    #Iterate over each LED in the strip

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

        # 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(100)

圖形化編程

對應(yīng)的mpy代碼:
 

import neopixel
import random
from microbit import *


np = neopixel.NeoPixel(pin0, 8)
while True:
  np[(random.randint(0, 7))] = ((random.randint(1, 50)), (random.randint(1, 50)), (random.randint(1, 50)))
  np.show()
  sleep(100)

如果直接用 microbit/newbit的3.3V供電,注意不要控制太多LED,因?yàn)長DO的輸出功率有限,很容易造成過熱保護(hù)。超過8個(gè)LED最好就用外部電源。

讀到這里,這篇“MicroPython neopixle怎么用”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI