changed 3 years ago
Published Linked with GitHub

micropython WS2812使用紀錄

LHB阿好伯, 2021/07/16

tags: Micropython

from machine import Pin from neopixel import NeoPixel import time pin = Pin(4, Pin.OUT) # 將GPIO4設置為輸出以驅動Neopixels ws2812 = NeoPixel(pin, 12) # 在GPIO0上以8像素創建NeoPixel驅動程序 ws2812[0] = (50, 50, 50) # 將第一個ws2812設置為白色 #(R,G,B) 0~255 r, g, b = ws2812[0] # 獲得第一個ws2812顏色 print(r, g, b) ws2812.write() # 將數據寫入ws2812 for i in range(12): ws2812[i] = (1*i, 1, 1) ws2812.write() time.sleep(1) # 填充所有顏色 def fill_color(r, g, b, n): #n 燈泡數 for i in range(n): ws2812[i] = (r, g, b) ws2812.write() fill_color(10, 10, 10, 12) time.sleep(1)

呼吸燈

from machine import Pin from neopixel import NeoPixel import time pin = Pin(4, Pin.OUT) # 將GPIO4設置為輸出以驅動Neopixels ws2812 = NeoPixel(pin, 12) # 在GPIO0上以8像素創建NeoPixel驅動程序 # 填充所有顏色 def fill_color(r, g, b, n): #n 燈泡數 for i in range(n): ws2812[i] = (r, g, b) ws2812.write() # 呼吸燈 def breathing_light(r, g, b, n, wait): while True: for i in range(100): fill_color(int(r*i/100), int(g*i/100), int(b*i/100), n) time.sleep_ms(wait) print(i/100) for i in range(100): fill_color(int(r*(1-i/100)), int(g*(1-i/100)), int(b*(1-i/100)), n) print(1-i/100) time.sleep_ms(wait) breathing_light(50, 50, 50, 12, 10)

彩虹燈

from machine import Pin from neopixel import NeoPixel import time pin = Pin(4, Pin.OUT) # 將GPIO4設置為輸出以驅動Neopixels ws2812 = NeoPixel(pin, 12) # 在GPIO0上以8像素創建NeoPixel驅動程序 # 彩虹燈條 def wheel(pos): #Input a value 0 to 255 to get a color value. #The colours are a transition r - g - b - back to r. if pos < 0 or pos > 255: return (0, 0, 0) if pos < 85: return (255 - pos * 3, pos * 3, 0) if pos < 170: pos -= 85 return (0, 255 - pos * 3, pos * 3) pos -= 170 return (pos * 3, 0, 255 - pos * 3) def rainbow_cycle(n, wait): for j in range(255): for i in range(n): rc_index = (i * 256 // n) + j ws2812[i] = wheel(rc_index & 255) ws2812.write() time.sleep_ms(wait) rainbow_cycle(12,50) #rainbow_cycle(n, wait) n 燈泡數 wait 間距毫秒數

🌟全文可以至下方連結觀看或是補充

全文分享至

https://www.facebook.com/LHB0222/

https://www.instagram.com/ahb0222/

有疑問想討論的都歡迎於下方留言

喜歡的幫我分享給所有的朋友 \o/

有所錯誤歡迎指教

全部文章列表

Select a repo