Skip to content

Commit 5842527

Browse files
committed
CPython Code for Crystal Gems
created code.py
1 parent 6006bda commit 5842527

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

PaperCraft_Gems/code.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
Crystal Gem Light Strand Project By Erin St Blaine for Adafruit Industries
3+
https://learn.adafruit.com/no-solder-papercraft-crystal-light-strand
4+
5+
Circuit Playground Bluetooth with LED strand attached runs 4 different variable animation modes.
6+
7+
Code by Roy Hooper using Adafruit's LED Animation Library:
8+
https://learn.adafruit.com/circuitpython-led-animations/overview
9+
"""
10+
# pylint: disable=attribute-defined-outside-init
11+
12+
#Import libraries
13+
import board
14+
import neopixel
15+
from adafruit_circuitplayground import cp
16+
from adafruit_led_animation.animation.solid import Solid
17+
from adafruit_led_animation.animation import Animation
18+
from adafruit_led_animation.animation.rainbow import Rainbow
19+
from adafruit_led_animation.animation.sparkle import Sparkle
20+
from adafruit_led_animation.sequence import AnimationSequence
21+
from adafruit_led_animation.color import WHITE, colorwheel
22+
23+
speeds = (0.25, 0.125, 0.1, 0.08, 0.05, 0.02, 0.01) # Customize speed levels here
24+
# periods = (7, 6, 5, 4, 3, 2, 1)
25+
26+
class RainbowFade(Animation):
27+
28+
_color_index = 0
29+
def __init__(self, pixel_object, speed, name):
30+
super().__init__(pixel_object, speed=speed, color=WHITE, name=name)
31+
32+
def draw(self):
33+
self.color = colorwheel(self._color_index)
34+
self._color_index = (self._color_index + 1) % 256
35+
self.fill(self.color)
36+
37+
38+
current_speed = 4
39+
40+
# Set your number of pixels here
41+
pixel_num = 20
42+
43+
pixel_pin = board.A1
44+
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False)
45+
46+
# Animation Setup
47+
48+
rainbow = Rainbow(pixels, speed=speeds[current_speed], period=2, name="rainbow", step=3)
49+
sparkle = Sparkle(pixels, speed=speeds[current_speed], color=WHITE, name="sparkle")
50+
rainbowfade = RainbowFade(pixels, speed=speeds[current_speed], name="rainbowfade")
51+
solid = Solid(pixels, color=colorwheel(0), name="solid")
52+
53+
# Animation Sequence Playlist -- rearrange to change the order of animations
54+
55+
animations = AnimationSequence(
56+
rainbow,
57+
rainbowfade,
58+
solid,
59+
sparkle,
60+
auto_clear=True,
61+
auto_reset=True,
62+
)
63+
64+
solid.speed = 0.01
65+
solid_color = 0
66+
67+
while True:
68+
if cp.switch: # if slide switch is in the "on" position, run animations
69+
animations.animate() #play animation sequence
70+
if cp.button_a:
71+
animations.next()
72+
while cp.button_a:
73+
continue
74+
75+
if cp.button_b:
76+
if animations.current_animation.name == "solid":
77+
solid_color = (solid_color + 8) % 256
78+
animations.current_animation.color = colorwheel(solid_color)
79+
else:
80+
current_speed += 1
81+
if current_speed >= len(speeds):
82+
current_speed = 0
83+
rainbow.speed = speeds[current_speed]
84+
sparkle.speed = speeds[current_speed]
85+
rainbowfade.speed = speeds[current_speed]
86+
print(speeds[current_speed])
87+
while cp.button_b:
88+
continue
89+
else: # If slide switch is in the "off" position, set pixels to black
90+
pixels.fill(0)
91+
pixels.show()

0 commit comments

Comments
 (0)