本文小編為大家詳細介紹“MicroPython neopixle怎么用”,內容詳細,步驟清晰,細節處理妥當,希望這篇“MicroPython neopixle怎么用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
microbit/newbit的MicroPython固件中,內置了neopixel彩燈的控制,我們可以使用任意一個GPIO去控制neopixel,支持任意數量的彩燈。
import neopixel np = neopixel.NeoPixel(pin1, 8) np[0] = (0, 0, 200) np[1] = (0, 50, 100) np[2] = (200, 0, 0) np.show()
函數 neopixel.NeoPixel(PIN, NUM) 用來創建 neopixel 對象,它有兩個參數,第一個是GPIO,第二個是彩燈的數量。
neopixel 對象是一個元組列表,每個列表項都是由 RGB 三種顏色組成的元組。RGB參數的范圍是 0-255,三種顏色組合起來就有 256 x 256 x 256 = 1.67M種顏色。
顏色參數寫入列表后并不能改變彩燈,還需要調用函數 show(),才會更新。如果要清除彩燈,可以調用函數 clear().
官方的例子,隨機顯示彩燈。
""" 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)
圖形化編程
對應的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,因為LDO的輸出功率有限,很容易造成過熱保護。超過8個LED最好就用外部電源。
讀到這里,這篇“MicroPython neopixle怎么用”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。