Skip to content

Instantly share code, notes, and snippets.

@Neradoc
Created April 21, 2023 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neradoc/55251a5923c2b95edd8dff729c293b57 to your computer and use it in GitHub Desktop.
Save Neradoc/55251a5923c2b95edd8dff729c293b57 to your computer and use it in GitHub Desktop.
Xiao RP2040 pixel fade in CP
import time
import neopixel
import board
import math
import adafruit_rgbled
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.fill((255, 0, 255))
rgb_led = adafruit_rgbled.RGBLED(board.LED_RED, board.LED_GREEN, board.LED_BLUE, invert_pwm=True)
rgb_led.color = (255, 0, 255)
DELAY = 0.02
while True:
# sine (cosine) fade
print("Sine")
rgb_led.color = (255,0,255)
pixel.fill((255, 0, 255))
for _ in range(2):
for i in range(100):
rgb_led.color = tuple(
int(x * (1 - abs(math.cos(math.pi * i / 100))) / 4)
for x in (255,0,255)
)
pixel.brightness = (1 - abs(math.cos(math.pi * i / 100))) / 4
time.sleep(DELAY)
# square fade
print("Square")
rgb_led.color = (0, 255, 0)
pixel.fill((0, 255, 0))
for _ in range(2):
for i in range(0,50):
pixel.brightness = (i**2) / 100 / 100
rgb_led.brightness = rgb_led.color = tuple(
int(x * (i**2) / 100 / 100)
for x in (0,255,0)
)
time.sleep(DELAY)
for i in range(50,0,-1):
pixel.brightness = (i**2) / 100 / 100
rgb_led.brightness = rgb_led.color = tuple(
int(x * (i**2) / 100 / 100)
for x in (0,255,0)
)
time.sleep(DELAY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment