raspberryPiModuleExpansionT.../tutorial/6-1.ws2812_6leds_neopixel.py

55 lines
1.3 KiB
Python

import time
import board
import neopixel
# LED 설정
LED_COUNT = 6 # LED 개수
LED_PIN = board.D18 # GPIO 18번 (PWM 지원 핀)
ORDER = neopixel.GRB # WS2812B는 GRB 순서
# NeoPixel 객체 생성
pixels = neopixel.NeoPixel(
LED_PIN, LED_COUNT, brightness=1.0, auto_write=False, pixel_order=ORDER
)
# 색상 설정 함수
def color_wipe(color, wait=0.1):
for i in range(LED_COUNT):
pixels[i] = color
pixels.show()
time.sleep(wait)
# 무지개 색상 함수
def wheel(pos):
if pos < 85:
return (pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return (255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return (0, pos * 3, 255 - pos * 3)
def rainbow_cycle(wait=0.01):
for j in range(255):
for i in range(LED_COUNT):
pixel_index = (i * 256 // LED_COUNT) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
time.sleep(wait)
# 메인 루프
try:
while True:
color_wipe((255, 0, 0)) # 빨강
time.sleep(1)
color_wipe((0, 255, 0)) # 초록
time.sleep(1)
color_wipe((0, 0, 255)) # 파랑
time.sleep(1)
rainbow_cycle()
except KeyboardInterrupt:
pixels.fill((0, 0, 0))
pixels.show()