-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfastled-patterns.ino
More file actions
288 lines (227 loc) · 5.46 KB
/
fastled-patterns.ino
File metadata and controls
288 lines (227 loc) · 5.46 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 240
#define DATA_PIN 6
#define FORWARD 0
#define BACKWARD 1
#define SLOW 250
#define MEDIUM 50
#define FAST 5
CRGB leds[NUM_LEDS];
boolean direction = FORWARD;
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop() {
rainbow(0,NULL);
delay(3000);
colorWipe(CRGB::Black,FORWARD,FAST);
allRandom();
delay(3000);
disolve(15,100,MEDIUM);
for(int i=0; i<3; i++){
CRGB c1 = randomColor();
CRGB c2 = randomColor();
stripes(c1,c2,5);
delay(2000);
stripes(c2,c1,5);
delay(2000);
}
for(int i=0; i<2; i++){
cylon(randomColor(), 10,FAST);
}
lightning(NULL,15,50,MEDIUM);
lightning(CRGB::White,20,50,MEDIUM);
for(int i=0; i<3; i++){
theaterChase(randomColor(),10,SLOW);
}
theaterChaseRainbow(1,MEDIUM);
rainbow(FAST,1);
flash(randomColor(),10,SLOW);
flash(NULL,50,MEDIUM);
for(int i=0; i<2; i++){
colorWipe(randomColor(),FAST,direction);
direction = !direction;
}
}
// Changes all LEDS to given color
void allColor(CRGB c){
for(int i=0; i<NUM_LEDS; i++){
leds[i] = c;
}
FastLED.show();
}
void allRandom(){
for(int i=0; i<NUM_LEDS; i++){
leds[i] = randomColor();
}
FastLED.show();
}
// Random disolve colors
void disolve(int simultaneous, int cycles, int speed){
for(int i=0; i<cycles; i++){
for(int j=0; j<simultaneous; j++){
int idx = random(NUM_LEDS);
leds[idx] = CRGB::Black;
}
FastLED.show();
delay(speed);
}
allColor(CRGB::Black);
}
// Flashes given color
// If c==NULL, random color flash
void flash(CRGB c, int count, int speed){
for(int i=0; i<count; i++){
if(c){
allColor(c);
}
else{
allColor(randomColor());
}
delay(speed);
allColor(CRGB::Black);
delay(speed);
}
}
// Wipes color from end to end
void colorWipe(CRGB c, int speed, int direction){
for(int i=0; i<NUM_LEDS; i++){
if(direction == FORWARD){
leds[i] = c;
}
else{
leds[NUM_LEDS-1-i] = c;
}
FastLED.show();
delay(speed);
}
}
// Rainbow colors that slowly cycle across LEDs
void rainbow(int cycles, int speed){ // TODO direction
if(cycles == 0){
for(int i=0; i< NUM_LEDS; i++) {
leds[i] = Wheel(((i * 256 / NUM_LEDS)) & 255);
}
FastLED.show();
}
else{
for(int j=0; j<256*cycles; j++) {
for(int i=0; i< NUM_LEDS; i++) {
leds[i] = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
}
FastLED.show();
delay(speed);
}
}
}
// Theater-style crawling lights
void theaterChase(CRGB c, int cycles, int speed){ // TODO direction
for (int j=0; j<cycles; j++) {
for (int q=0; q < 3; q++) {
for (int i=0; i < NUM_LEDS; i=i+3) {
int pos = i+q;
leds[pos] = c; //turn every third pixel on
}
FastLED.show();
delay(speed);
for (int i=0; i < NUM_LEDS; i=i+3) {
leds[i+q] = CRGB::Black; //turn every third pixel off
}
}
}
}
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed){ // TODO direction, duration
for (int j=0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < NUM_LEDS; i=i+3) {
int pos = i+q;
leds[pos] = Wheel( (i+j) % 255); //turn every third pixel on
}
FastLED.show();
delay(speed);
for (int i=0; i < NUM_LEDS; i=i+3) {
leds[i+q] = CRGB::Black; //turn every third pixel off
}
}
}
}
// Random flashes of lightning
void lightning(CRGB c, int simultaneous, int cycles, int speed){
int flashes[simultaneous];
for(int i=0; i<cycles; i++){
for(int j=0; j<simultaneous; j++){
int idx = random(NUM_LEDS);
flashes[j] = idx;
leds[idx] = c ? c : randomColor();
}
FastLED.show();
delay(speed);
for(int s=0; s<simultaneous; s++){
leds[flashes[s]] = CRGB::Black;
}
delay(speed);
}
}
// Sliding bar across LEDs
void cylon(CRGB c, int width, int speed){
// First slide the leds in one direction
for(int i = 0; i <= NUM_LEDS-width; i++) {
for(int j=0; j<width; j++){
leds[i+j] = c;
}
FastLED.show();
// now that we've shown the leds, reset to black for next loop
for(int j=0; j<5; j++){
leds[i+j] = CRGB::Black;
}
delay(speed);
}
// Now go in the other direction.
for(int i = NUM_LEDS-width; i >= 0; i--) {
for(int j=0; j<width; j++){
leds[i+j] = c;
}
FastLED.show();
for(int j=0; j<width; j++){
leds[i+j] = CRGB::Black;
}
delay(speed);
}
}
// Display alternating stripes
void stripes(CRGB c1, CRGB c2, int width){
for(int i=0; i<NUM_LEDS; i++){
if(i % (width * 2) < width){
leds[i] = c1;
}
else{
leds[i] = c2;
}
}
FastLED.show();
}
// Theater-style crawling of stripes
void stripesChase(CRGB c1, CRGB c2, int width, int cycles, int speed){ // TODO direction
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
CRGB Wheel(byte WheelPos) {
if(WheelPos < 85) {
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if(WheelPos < 170) {
WheelPos -= 85;
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
CRGB randomColor(){
return Wheel(random(256));
}