-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstranger-things.ino
218 lines (203 loc) · 5.46 KB
/
stranger-things.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "FastLED/FastLED.h"
FASTLED_USING_NAMESPACE;
#define LED_TYPE WS2811 // Set to your led type (see FastLED)
#define NUM_LEDS 100 // Set to the number of leds in your string
#define DATA_PIN 6 // Wire your leds to this digital pin
#define SKIP_INDEX -1
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 120
CRGB leds[NUM_LEDS];
bool flickerOn = true;
bool idleMode = true;
void setup() {
FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
// Set up web control
Particle.function("led",ledToggle);
}
void loop() {
setColors();
if (flickerOn) {
addFlicker(30);
}
FastLED.show();
FastLED.delay(1000/FRAMES_PER_SECOND);
// In "Idle Mode," occasionally spell out messages
if (idleMode) {
int r = random(0,10000);
if (r == 1) {
spell("Happy Halloween");
} else if (r == 2) {
spell("Help me");
} else if (r == 3) {
spell("Are you there");
} // Add your own messages here
}
}
// Web control function
int ledToggle(String command)
{
if (command=="setupMode") {
setupMode();
return 1;
} else if (command=="idleOn") {
// Turn on randomly-occurring messages
idleMode = true;
} else if (command=="idleOff") {
// Turn off randomly-occurring messages,
// e.g., to ensure only stream of message does not get interrupted
idleMode = false;
} else if (command=="flickerOn") {
flickerOn = true;
} else if (command=="flickerOff") {
// Turn the flickering off -- may be better for some lighting conditions
flickerOn = false;
} else if (command=="glowdown") {
glowdown();
return 1;
} else if (command == "off") {
FastLED.clear();
FastLED.show();
return 1;
} else {
// Spell out unrecognized commands
spell(command);
return 1;
}
}
// Colors based on show screencaps
void setColors() {
int color_count = 7;
CRGB colors[7] = {
CRGB::DarkBlue,
CRGB::DeepPink,
CRGB::LightGreen,
CRGB::DeepSkyBlue,
CRGB::DarkOrange,
CRGB::Fuchsia,
CRGB::Turquoise
};
for (int i = 0; i < color_count; ++i) {
for (int j = i; j < NUM_LEDS; j += color_count) {
leds[j] = colors[i];
}
}
}
void addFlicker(int chanceOfFlicker)
{
if( random(0,100) < chanceOfFlicker) {
leds[ random16(NUM_LEDS) ] -= CRGB::White;
}
}
// Gradually lower to the lights for dramatic effect (usually before spelling)
void glowdown() {
for (int i = 100; i > 0; --i){
for(int x = 0; x < NUM_LEDS; ++x){
leds[x].fadeLightBy( 32 ); // 12.5%
}
FastLED.show();
delay(50);
}
}
void glowLight(int i, int duration)
{
CRGB current = leds[i];
leds[i].maximizeBrightness();
FastLED.show();
delay(duration);
leds[i] = current;
FastLED.show();
}
// Lower the lights dramatically, then spell out the String by glowing each
// letter one by one
void spell(String str)
{
glowdown();
str.toLowerCase();
for (int i = 0; i < str.length(); ++i) {
char tmp = str[i];
int j = getLetterInt(tmp);
if (j == SKIP_INDEX) {
continue;
}
// Randomize duration of lights and delay between lights for a little
// extra organic/creepy factor
glowLight(j % NUM_LEDS, random(3, 13) * 100);
delay(random(3, 13) * 100);
}
}
// Very memory-limited, so better to do a bunch of ifs than store a map (sorry)
// Organized by light order for easier adgustment
int getLetterInt(char letter) {
// First row - left to right
if (letter == 'a') {
return 97;
} else if (letter == 'b') {
return 95;
} else if (letter == 'c') {
return 92;
} else if (letter == 'd') {
return 90;
} else if (letter == 'e') {
return 87;
} else if (letter == 'f') {
return 83;
} else if (letter == 'g') {
return 81;
} else if (letter == 'h') {
return 76;
// Second row - right to left
} else if (letter == 'q') {
return 72;
} else if (letter == 'p') {
return 69;
} else if (letter == 'o') {
return 67;
} else if (letter == 'n') {
return 64;
} else if (letter == 'm') {
return 61;
} else if (letter == 'l') {
return 59;
} else if (letter == 'k') {
return 55;
} else if (letter == 'j') {
return 54;
} else if (letter == 'i') {
return 51;
// Third row - left to right
} else if (letter == 'r') {
return 43;
} else if (letter == 's') {
return 41;
} else if (letter == 't') {
return 39;
} else if (letter == 'u') {
return 36;
} else if (letter == 'v') {
return 34;
} else if (letter == 'w') {
return 30;
} else if (letter == 'x') {
return 27;
} else if (letter == 'y') {
return 25;
} else if (letter == 'z') {
return 22;
}
return SKIP_INDEX; // out of alphabet
}
// Light all letters of the alphabet to make
// physical alignment of letters easier
void setupMode()
{
String str = "abcdefghqponmlkjirstuvwxyz";
CRGB color = CRGB::White;
for (int i = 0; i < str.length(); ++i) {
char tmp = str[i];
int j = getLetterInt(tmp);
leds[j] = color;
color -= CRGB(0,13,13);
}
FastLED.show();
}