Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Last active April 26, 2020 22:35
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 FoamyGuy/1201041c5c4cfb2ed645c990a345dafc to your computer and use it in GitHub Desktop.
Save FoamyGuy/1201041c5c4cfb2ed645c990a345dafc to your computer and use it in GitHub Desktop.
# Modified 'Cyber falls' sketch: https://learn.adafruit.com/neopixel-cyber-falls/circuitpython-code
#
# This version expects to execute one step of he animation
# at a time. This makes it easier to integrate with other animations
# running simultaneously.
import time
import board
import neopixel
import adafruit_fancyled.adafruit_fancyled as fancy
import pulseio
from digitalio import DigitalInOut, Direction, Pull
num_leds = 20 # number of LEDs per strip
saturation = 255 # 0-255, 0 is pure white, 255 is fully saturated color
blend = True # color blending between palette indices
brightness = 0.1 # half brightness the range is 0.0 - 1.0
concurrent = 5 # number of LEDs on at a time
on_time = 0.04 # 0.04 seconds == 40 milliseconds
# NeoPixel object
drop0 = neopixel.NeoPixel(board.A1, num_leds)
GREEN_BLINK_ON = 7.0
GREEN_BLINK_OFF = 0.2
RED_BLINK_ON = 0.33
RED_BLINK_OFF = 4.0
PULSE_DURATION = 0.015
pwm_leds = board.D5
pwm = pulseio.PWMOut(pwm_leds, frequency=1000, duty_cycle=0)
GREEN_LED = DigitalInOut(board.D12)
GREEN_LED.direction = Direction.OUTPUT
RED_LED = DigitalInOut(board.D9)
RED_LED.direction = Direction.OUTPUT
pwm_brightness = 0 # how bright the LED is
fade_amount = 1285 # 2% steping of 2^16
def led_drops(strip, iteration_step):
# FancyLED allows for mixing colors with palettes
palette = [fancy.CRGB(0, 0, 255), # lighter (more white) green
fancy.CRGB(0, 255, 0)] # full green
i = iteration_step % num_leds
# FancyLED can handle the gamma adjustment, brightness and RGB settings
color = fancy.palette_lookup(palette, i / num_leds)
color = fancy.gamma_adjust(color, brightness=brightness)
strip[i] = color.pack()
# turn off the LEDs as we go for raindrop effect
if i >= concurrent:
strip[i - concurrent] = (0,0,0)
if i == num_leds - 1:
strip[num_leds-concurrent] = (0,0,0)
if i < concurrent:
strip[num_leds-(concurrent-i)] = (0,0,0)
iteration_counter = 0
next_neopixel_action = 0
now = time.monotonic()
need_to_change_red_time = now + RED_BLINK_ON
need_to_change_green_time = now + GREEN_BLINK_OFF
need_to_pulse_action = now + PULSE_DURATION
while True:
now = time.monotonic()
if now >= next_neopixel_action:
led_drops(drop0, iteration_counter)
next_neopixel_action = now + on_time
iteration_counter += 1
if iteration_counter >= num_leds:
iteration_counter = 0
if now >= need_to_change_red_time:
#print("red animation step at: {}".format(now))
RED_LED.value = not RED_LED.value
if RED_LED.value:
need_to_change_red_time = now + RED_BLINK_ON
else:
need_to_change_red_time = now + RED_BLINK_OFF
if now >= need_to_change_green_time:
#print("green animation step at: {}".format(now))
GREEN_LED.value = not GREEN_LED.value
if GREEN_LED.value:
need_to_change_green_time = now + GREEN_BLINK_ON
else:
need_to_change_green_time = now + GREEN_BLINK_OFF
if now >= need_to_pulse_action:
# And send to LED as PWM level
pwm.duty_cycle = pwm_brightness
# change the brightness for next time through the loop:
pwm_brightness = pwm_brightness + fade_amount
# reverse the direction of the fading at the ends of the fade:
if pwm_brightness <= 0:
fade_amount = -fade_amount
elif pwm_brightness >= 65535:
fade_amount = -fade_amount
need_to_pulse_action = now + PULSE_DURATION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment