Created
April 21, 2023 11:10
-
-
Save Neradoc/55251a5923c2b95edd8dff729c293b57 to your computer and use it in GitHub Desktop.
Xiao RP2040 pixel fade in CP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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