from machine import Pin
import math
import time
import urandom as random
from neopixel import Neopixel
# 矩陣設定
width = 8
height = 8
NUMPIXELS = width * height # NeoPixel size
ws_pin = 13
# 彩虹漸變效果
# 這個效果會在矩陣上顯示一個逐漸變化的彩虹色。
def rainbow_effect():
for i in range(NUMPIXELS):
# 計算 RGB 值
red = int((math.sin(i / 8 + 0) * 127 + 128))
green = int((math.sin(i / 8 + 2) * 127 + 128))
blue = int((math.sin(i / 8 + 4) * 127 + 128))
pixels.set_pixel(i, (red, green, blue))
pixels.show()
# 脈衝燈效(Pulse Effect)
# 這種效果讓 LED 顯示一種"呼吸"的脈衝感。
def pulse_effect():
for brightness in range(0, 1024, 10): # 從 0 到 1023(脈衝增亮)
color = (brightness, brightness, brightness) # 灰度脈衝
for i in range(NUMPIXELS):
pixels.set_pixel(i, color)
pixels.show()
time.sleep(0.01)
for brightness in range(1023, -1, -10): # 從 1023 降到 0(脈衝暗淡)
color = (brightness, brightness, brightness)
for i in range(NUMPIXELS):
pixels.set_pixel(i, color)
pixels.show()
time.sleep(0.01)
# 色環函數(Hue 轉 RGB)
def Wheel(WheelPos):
if WheelPos < 0 or WheelPos > 255:
return (0, 0, 0)
elif WheelPos < 85:
return (255 - WheelPos * 3, WheelPos * 3, 0)
elif WheelPos < 170:
WheelPos -= 85
return (0, 255 - WheelPos * 3, WheelPos * 3)
else:
WheelPos -= 170
return (WheelPos * 3, 0, 255 - WheelPos * 3)
# 顯示彩虹漸變效果
def rainbow_cycle(wait=20):
j = 0
while True:
for y in range(height):
for x in range(width):
pixel_index = y * width + x # 單行排列
color = Wheel((x * 256 // width + j) & 255)
pixels.set_pixel(pixel_index, color)
pixels.show()
time.sleep_ms(wait)
j = (j + 1) % 256
# def rainbow_cycle(wait=50):
# """整個矩陣播放彩虹漸變動畫"""
# while True:
# for j in range(256): # 0~255 彩虹循環一次
# for i in range(NUMPIXELS):
# pixel_index = (i * 256 // NUMPIXELS + j) & 255
# pixels.set_pixel(i, Wheel(pixel_index))
# pixels.show()
# time.sleep_ms(wait)
# 彩虹波紋 Rainbow Ripple
# 像一圈一圈擴散出去的彩虹波。
def rainbow_ripple(wait=50):
for j in range(256):
for y in range(8):
for x in range(8):
index = (x + y * 8)
d = math.sqrt((x - 3.5)**2 + (y - 3.5)**2)
color = Wheel(int((d * 16 + j)) % 256)
pixels.set_pixel(index, color)
pixels.show()
time.sleep_ms(wait)
# 旋轉彩虹圈 Rainbow Spiral
# 用數學方式做出一個旋轉色彩漩渦感。
def rainbow_spiral(wait=50):
center_x, center_y = 3.5, 3.5
for t in range(256):
for y in range(8):
for x in range(8):
dx = x - center_x
dy = y - center_y
angle = math.atan2(dy, dx)
distance = math.sqrt(dx * dx + dy * dy)
color = Wheel(int((angle / math.pi + 1) * 128 + t + distance * 10) % 256)
pixels.set_pixel(y * 8 + x, color)
pixels.show()
time.sleep_ms(wait)
# 彩色呼吸 Rainbow Breathing
# 整片矩陣會慢慢淡入淡出變色,像在呼吸。
def rainbow_breathing(wait=20):
for b in range(0, 256, 4):
color = Wheel(b)
for i in range(NUMPIXELS):
pixels.set_pixel(i, color)
pixels.show()
time.sleep_ms(wait)
for b in range(255, -1, -4):
color = Wheel(b)
for i in range(NUMPIXELS):
pixels.set_pixel(i, color)
pixels.show()
time.sleep_ms(wait)
# 彩虹掃描線 Rainbow Scanning
# 像是一條彩色橫線在上面掃描、變色。
def rainbow_scanner(wait=50):
for row in range(8):
for i in range(NUMPIXELS):
pixels.set_pixel(i, (0, 0, 0))
for col in range(8):
index = row * 8 + col
color = Wheel((row * 32 + col * 4) % 256)
pixels.set_pixel(index, color)
pixels.show()
time.sleep_ms(wait)
# 彩虹追光 Rainbow Chaser
# 像是彩虹的蛇從左到右跑動過去。
def rainbow_chaser(wait=50):
for shift in range(NUMPIXELS):
for i in range(NUMPIXELS):
color = Wheel((i + shift) % 256)
pixels.set_pixel(i, color)
pixels.show()
time.sleep_ms(wait)
# Start Function
if __name__ == '__main__':
print('NeoMatrix 8X8 彩虹特效')
# Create a NeoPixel object
pixels = Neopixel(NUMPIXELS, 0, ws_pin, "GRB")
while True:
print('彩虹追光')
rainbow_chaser(20)
print('彩虹波紋')
rainbow_ripple(20)
print('彩色呼吸')
rainbow_breathing(20)
print('彩虹掃描線')
rainbow_scanner(100)
print('旋轉彩虹圈')
rainbow_spiral(20)
print('彩虹漸變效果')
rainbow_effect()
time.sleep(2)
print('彩虹特效')
rainbow_cycle(50)
# 調用脈衝效果
# pulse_effect()