Skip to content

Commit 420d0e8

Browse files
authored
Merge pull request #1254 from firepixie/master
Bluetooth Room Lights Code
2 parents f290b4a + 279ea10 commit 420d0e8

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

Bluetooth_Room_Lights/code.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
"""
2+
Bluetooth Controlled Room Lights using a Circuit Playground Bluetooth
3+
Scroll between 7 modes and control brightness with your smartphone via Bluetooth
4+
Full tutorial: https://learn.adafruit.com/easy-bluetooth-controlled-room-lights/overview
5+
Code by Kattni Rembor & Erin St Blaine for Adafruit Industries
6+
Adafruit invests time and resources to bring you this code! Please support our shop!
7+
"""
8+
9+
# pylint: disable=attribute-defined-outside-init
10+
# pylint: disable=too-few-public-methods
11+
12+
import board
13+
import neopixel
14+
from adafruit_led_animation.animation.solid import Solid
15+
from adafruit_led_animation.animation.comet import Comet
16+
from adafruit_led_animation.animation.rainbow import Rainbow
17+
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
18+
from adafruit_led_animation.animation.sparkle import Sparkle
19+
from adafruit_led_animation.animation.sparklepulse import SparklePulse
20+
from adafruit_led_animation.sequence import AnimationSequence
21+
from adafruit_led_animation.group import AnimationGroup
22+
from adafruit_led_animation.animation import Animation
23+
from adafruit_led_animation.sequence import AnimateOnce
24+
from adafruit_led_animation.color import (
25+
AMBER,
26+
ORANGE,
27+
WHITE,
28+
RED,
29+
BLACK,
30+
colorwheel,
31+
)
32+
33+
34+
from adafruit_ble import BLERadio
35+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
36+
from adafruit_ble.services.nordic import UARTService
37+
38+
from adafruit_bluefruit_connect.packet import Packet
39+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
40+
from adafruit_bluefruit_connect.color_packet import ColorPacket
41+
42+
NUM_LEDS = 240 # change to reflect your LED strip
43+
NEOPIXEL_PIN = board.A1 # change to reflect your wiring
44+
45+
# Declare a NeoPixel object on NEOPIXEL_PIN with NUM_LEDS pixels,
46+
# no auto-write.
47+
# Set brightness to max, we'll control it later in the code
48+
pixels = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_LEDS, brightness=1.0,
49+
auto_write=False,
50+
#pixel_order=(1,0,2,3) #uncomment if using RGBW NeoPixels
51+
)
52+
53+
54+
ble = BLERadio()
55+
uart_service = UARTService()
56+
advertisement = ProvideServicesAdvertisement(uart_service)
57+
58+
class RainbowFade(Animation):
59+
''' fades the entire strip through the whole spectrum '''
60+
_color_index = 150 # choose start color (0-255)
61+
def __init__(self, pixel_object, speed, name): # define animation
62+
super().__init__(pixel_object, speed=speed, color=WHITE, name=name)
63+
64+
def draw(self): # draw the animation
65+
''' fades the entire strip through the whole spectrum '''
66+
self.color = colorwheel(self._color_index + 1)
67+
self._color_index = (self._color_index + 1) % 256
68+
self.fill(self.color)
69+
70+
# ANIMATION DEFINITIONS --
71+
# create as many animations as you'd like and define their attributes here.
72+
# They can be a single line or a group of animations - the groups will play
73+
# at the same time, overlaid on top of each other.
74+
75+
76+
readingLight = Solid(pixels, color=0xFF7D13) #warm white color HEX code
77+
brightWhite = Solid(pixels, color=(150, 150, 150))
78+
rainbow = Rainbow(pixels, speed=0.1, period=10, step=0.5)
79+
rainbowfade = RainbowFade(pixels, speed=0.4, name="rainbowfade")
80+
powerup = RainbowComet(pixels, speed=0, tail_length=50, bounce=False)
81+
off = Solid(pixels, color=BLACK)
82+
83+
#startup animation will play just once
84+
startup = AnimateOnce(powerup)
85+
86+
#starrynight and fire are animation groups with layered effects.
87+
starrynight = AnimationGroup(
88+
SparklePulse(pixels, speed=0.01, color=(0, 0, 150), period=1),
89+
Comet(pixels, speed=0, tail_length=8, color=(150, 150, 150), bounce=False),)
90+
91+
fire = AnimationGroup(
92+
Comet(pixels, speed=0, tail_length=1, color=BLACK),
93+
Sparkle(pixels, speed=0.05, num_sparkles=10, color=AMBER),
94+
Sparkle(pixels, speed=0.05, num_sparkles=10, color=RED),
95+
Sparkle(pixels, speed=0.05, num_sparkles=20, color=ORANGE),
96+
Sparkle(pixels, speed=0.05, num_sparkles=5, color=0xFF7D13),
97+
Sparkle(pixels, speed=0.05, num_sparkles=10, color=BLACK),
98+
)
99+
100+
# Here is the animation playlist where you set the order of modes
101+
102+
animations = AnimationSequence(
103+
readingLight,
104+
fire,
105+
rainbow,
106+
starrynight,
107+
rainbowfade,
108+
brightWhite,
109+
auto_clear=True,
110+
)
111+
112+
113+
114+
MODE = 0
115+
116+
while True:
117+
if MODE == 0: # If currently off...
118+
startup.animate()
119+
while startup.animate():
120+
pass
121+
MODE = 1
122+
# Advertise when not connected
123+
124+
elif MODE >= 1: # If not OFF MODE...
125+
ble.start_advertising(advertisement)
126+
while not ble.connected:
127+
if MODE == 2:
128+
pass
129+
elif MODE == 1:
130+
animations.animate()
131+
# Now we're connected
132+
133+
while ble.connected:
134+
if uart_service.in_waiting:
135+
packet = Packet.from_stream(uart_service)
136+
# Color Picker Functionality
137+
if isinstance(packet, ColorPacket):
138+
MODE = 2
139+
# Set all the pixels to one color and stay there.
140+
pixels.fill(packet.color)
141+
pixels.show()
142+
# Control Pad Functionality
143+
elif isinstance(packet, ButtonPacket):
144+
if packet.pressed:
145+
if packet.button == ButtonPacket.BUTTON_1:
146+
MODE = 1
147+
animations.activate(1)
148+
elif packet.button == ButtonPacket.BUTTON_2:
149+
MODE = 1
150+
animations.activate(2)
151+
elif packet.button == ButtonPacket.BUTTON_3:
152+
MODE = 1
153+
animations.activate(3)
154+
elif packet.button == ButtonPacket.BUTTON_4:
155+
MODE = 1
156+
animations.activate(4)
157+
# change the mode with right arrow
158+
elif packet.button == ButtonPacket.RIGHT:
159+
MODE = 1
160+
animations.next()
161+
elif packet.button == ButtonPacket.LEFT:
162+
MODE = 4
163+
off.animate()
164+
#change the brightness with up and down arrows
165+
elif packet.button == ButtonPacket.UP:
166+
pixels.brightness = pixels.brightness + 0.1
167+
pixels.show()
168+
if pixels.brightness > 1:
169+
pixels.brightness = 1
170+
elif packet.button == ButtonPacket.DOWN:
171+
pixels.brightness = pixels.brightness - 0.1
172+
pixels.show()
173+
if pixels.brightness < 0.1:
174+
pixels.brightness = 0.1
175+
if MODE == 1:
176+
animations.animate()
177+
if MODE == 4:
178+
animations.freeze()

0 commit comments

Comments
 (0)