Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created August 21, 2020 01:12
Show Gist options
  • Save FoamyGuy/4fb2adab0a2e04a762deacb0ddc4a429 to your computer and use it in GitHub Desktop.
Save FoamyGuy/4fb2adab0a2e04a762deacb0ddc4a429 to your computer and use it in GitHub Desktop.
import board
import digitalio
import time
import neopixel
PWM_BLINK_RATE = 0.5 # seconds
LED_FADE_STEPS = 400 # 400 steps = 100 for each of:
# red_fade_up, red_fade_down, blue_fade_up, blue_fade_down
LED_FADE_TIME = 0.01 # seconds between steps of neopixel fading
PREV_PWM_TIME = 0 # monotonic timestamp of laste time pwm changed
PREV_LED_TIME = 0 # monotonic timestamp of laste time neopixel changed
pwm = digitalio.DigitalInOut(board.D5)
pwm.direction = digitalio.Direction.OUTPUT
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixels.brightness = 0.5
RED = (255, 0, 0)
BLUE = (0, 0, 255)
def current_fade_pixels_action(i):
if i < LED_FADE_STEPS/4:
pixels.brightness = ((i%100) * (1.0 / 100))
pixels.fill(RED)
elif i < LED_FADE_STEPS/2:
pixels.brightness = (1.0 - (i%100) * (1.0 / 100))
pixels.fill(RED)
elif i < (LED_FADE_STEPS/4) * 3:
pixels.brightness = ((i%100) * (1.0 / 100))
pixels.fill(BLUE)
elif i < LED_FADE_STEPS:
pixels.brightness = (1.0 - (i%100) * (1.0 / 100))
pixels.fill(BLUE)
#print("{} - {}".format(i, pixels.brightness))
i = 0
while True:
now = time.monotonic()
if PREV_PWM_TIME + PWM_BLINK_RATE <= now:
pwm.value = not pwm.value
PREV_PWM_TIME = now
if PREV_LED_TIME + LED_FADE_TIME <= now:
PREV_LED_TIME = now
current_fade_pixels_action(i)
i += 1
if i == LED_FADE_STEPS:
i = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment