from machine import Pin
import time
import urandom as random
from neopixel import Neopixel
import led_transform
ws_pin = 13
NUMPIXELS = 28 # NeoPixel size
pixDELAY = 50 # Delay for pixel persistence
GRN_Base = (NUMPIXELS // 4)
BLU_Base = (NUMPIXELS // 2)
YEL_Base = ((NUMPIXELS * 3) // 4)
def transform(index,color):
print(index)
if index <= 28:
pixels.set_pixel((led_transform.display1(index))-1, color)
elif index <= 48:
index = index - 28
pixels.set_pixel((led_transform.display2(index))-1, color)
else:
index = index - 48
pixels.set_pixel((led_transform.display3(index))-1, color)
# Function to generate a random color
def random_color():
return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
# Function to update the flowing light effect for NeoPixel LED ring
def update_flow():
for j in range(20):
for i in range(NUMPIXELS):
pixels.set_pixel((led_transform.display1(i+1))-1, random_color())
pixels.show() # Update the LED strip
time.sleep_ms(pixDELAY)
# Reference: https://wokwi.com/projects/405827644049669121
def walk(steps):
for _ in range(steps):
step()
paint()
time.sleep_ms(pixDELAY)
def step():
global x
global direction
if direction == 'left':
x = max(0, x - 1)
if x == 0:
direction = 'right'
elif direction == 'right':
x = min(NUMPIXELS-1, x + 1)
if x == NUMPIXELS-1:
direction = 'left'
for i in range(NUMPIXELS):
ring[i] = max(0, ring[i] - 10) # Fade out all positions
ring[x] = 100 # Mark the new position
def paint():
for i in range(NUMPIXELS):
pixels.set_pixel((led_transform.display1(i+1)-1), (0, ring[i], 0))
pixels.show() # Update pixel colors
# Reference: https://wokwi.com/projects/359664314470215681
def rainbow_animation():
for rotation in range(NUMPIXELS * 10): # Repeat the animation 10 times
for index in range(NUMPIXELS):
hue = (index + rotation) % NUMPIXELS / NUMPIXELS
pixels.set_pixel((led_transform.display1(index+1)-1), hsv_to_rgb(hue, 1, 1)) # Set the current pixel to the rainbow color
pixels.show() # Update pixel colors
time.sleep_ms(5) # Wait between changes
def hsv_to_rgb(h, s, v):
i = int(h * 6.)
f = (h * 6.) - i
p = v * (1. - s)
q = v * (1. - s * f)
t = v * (1. - s * (1. - f))
i = i % 6
if i == 0:
return int(v * 255), int(t * 255), int(p * 255)
elif i == 1:
return int(q * 255), int(v * 255), int(p * 255)
elif i == 2:
return int(p * 255), int(v * 255), int(t * 255)
elif i == 3:
return int(p * 255), int(q * 255), int(v * 255)
elif i == 4:
return int(t * 255), int(p * 255), int(v * 255)
elif i == 5:
return int(v * 255), int(p * 255), int(q * 255)
# Reference: https://wokwi.com/projects/338857849104892500
# 彩虹滾動圈圈
def rainbowCycle(wait):
for j in range(256 * 5):
for i in range(NUMPIXELS):
pixels.set_pixel((led_transform.display1(i+1)-1), Wheel(((i * 256 // NUMPIXELS) + j) & 255))
pixels.show()
time.sleep_ms(wait)
# 產生漸變顏色值
def Wheel(WheelPos):
WheelPos = 255 - WheelPos
if (WheelPos < 85) :
return [255 - WheelPos * 3, 0, WheelPos * 3]
if (WheelPos < 170) :
WheelPos = WheelPos - 85
return [0, WheelPos * 3, 255 - WheelPos * 3]
WheelPos = WheelPos - 170
return [WheelPos * 3, 255 - WheelPos * 3, 0]
# PassingColors
# Reference: https://wokwi.com/projects/384675870918916097
# by xfpd: https://wokwi.com/makers/xfpd
def collide():
global counter
# RED pixel follows the counter (at the bottom of this function)
RED = counter
# GREEN pixel starts at 1/4 (.25) position on the ring
GRN = GRN_Base - counter
if (GRN < 0) :
GRN = NUMPIXELS + GRN
# BLUE pixels starts at 1/2 (.5) position on the ring
BLU = BLU_Base + counter
if (BLU > NUMPIXELS - 1):
BLU = BLU - NUMPIXELS
# YELLOW pixel starts at 3/4 (.75) position on the ring
YEL = YEL_Base - counter
if (YEL < 0):
YEL = abs(NUMPIXELS + YEL)
print("RED %5d | GRN %5d | BLU %5d | YEL %5d" %(RED, GRN, BLU, YEL))
pixels.set_pixel((led_transform.display1(RED+1)-1), (255, 0, 0))
pixels.set_pixel((led_transform.display1(GRN+1)-1), (0, 255, 0))
pixels.set_pixel((led_transform.display1(BLU+1)-1), (0, 0, 255))
pixels.set_pixel((led_transform.display1(YEL+1)-1), (255, 255, 0))
pixels.show()
time.sleep_ms(pixDELAY)
# BLACK-out the trailing colored pixel
pixels.fill((0, 0, 0))
pixels.show()
if (counter < (NUMPIXELS-1)):
counter = counter + 1
else:
counter = 0
# Start Function
if __name__ == '__main__':
print('NeoPixel LED Ring 彩虹特效')
# Create a NeoPixel object
pixels = Neopixel(64, 0, ws_pin, "GRB")
counter = 0 # counter
while True:
update_flow() # flowing light effect
print('PassingColors')
for j in range(NUMPIXELS * 5):
collide()
ring = [0 for _ in range(NUMPIXELS)]
direction = random.choice(['left', 'right'])
x = NUMPIXELS//4
walk(NUMPIXELS*8)
print('rainbow_animation()')
rainbow_animation() # Bonus "Easter egg" animation
print('rainbowCycle()')
rainbowCycle(1) # 彩虹滾動圈圈