-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWS2812_TestPatterns.ino
More file actions
303 lines (245 loc) · 9.09 KB
/
WS2812_TestPatterns.ino
File metadata and controls
303 lines (245 loc) · 9.09 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*******************************************************************************
A little sketch to check the functionality of a WS2812 LED strip with an Arduino.
https://github.com/JoaDick/WS2812_TestPatterns
********************************************************************************
MIT License
Copyright (c) 2020 Joachim Dick
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#include <FastLED.h>
// Enable one of these according to your LED strip - see also setup().
#define LED_COLOR_ORDER RGB
//#define LED_COLOR_ORDER RBG
//#define LED_COLOR_ORDER GRB
//#define LED_COLOR_ORDER GBR
//#define LED_COLOR_ORDER BRG
//#define LED_COLOR_ORDER BGR
// Connect the LED Strip to that pin.
#define LED_PIN 6
// Connect a pushbutton for selecting the next pattern to that pin (and GND).
#define PIN_BUTTON_NEXT_PATTERN 2
// Connect a pushbutton for selecting the first pattern to that pin (and GND).
#define PIN_BUTTON_FIRST_PATTERN 3
//------------------------------------------------------------------------------
#define LED_TYPE WS2812B
#define NUM_LEDS 300
CRGB leds[NUM_LEDS];
bool lastButtonState = false;
uint8_t patternIndex = 0;
uint16_t animationCounter = 0;
uint8_t animationColorIndex = 0;
#define ARRAYLEN(x) (sizeof(x) / sizeof((x)[0]))
//------------------------------------------------------------------------------
void setup()
{
pinMode(PIN_BUTTON_NEXT_PATTERN, INPUT_PULLUP);
pinMode(PIN_BUTTON_FIRST_PATTERN, INPUT_PULLUP);
FastLED.addLeds<LED_TYPE, LED_PIN, LED_COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
Serial.begin(115200);
Serial.println(F(""));
Serial.println(F("WS2812 Test Patterns"));
Serial.println(F(""));
Serial.println(F("Verify LED_COLOR_ORDER setting:"));
Serial.println(F("One LED should be RED"));
Serial.println(F("Two LEDs should be GREEN"));
Serial.println(F("Three LEDs should be BLUE"));
Serial.println(F("If this is not the case, adjust LED_COLOR_ORDER according to the displayed colors."));
Serial.println(F(""));
// Taken from the FastLED RGB-Calibrate example:
// You should see six leds on. If the RGB ordering is correct, you should see 1 red led, 2 green
// leds, and 3 blue leds. If you see different colors, the count of each color tells you what the
// position for that color in the rgb orering should be. So, for example, if you see 1 Blue, and 2
// Red, and 3 Green leds then the rgb ordering should be BRG (Blue, Red, Green).
// You can then test this ordering by setting the RGB ordering in the #define LED_COLOR_ORDER ... line
// above to the new ordering and it should come out correctly, 1 red, 2 green, and 3 blue.
FastLED.clear();
leds[0] = CRGB{0x20, 0, 0};
leds[1] = CRGB{0, 0x20, 0};
leds[2] = CRGB{0, 0x20, 0};
leds[3] = CRGB{0, 0, 0x20};
leds[4] = CRGB{0, 0, 0x20};
leds[5] = CRGB{0, 0, 0x20};
FastLED.show();
delay(3000);
selectFirstPattern();
}
//------------------------------------------------------------------------------
void loop()
{
if (!digitalRead(PIN_BUTTON_FIRST_PATTERN))
{
selectFirstPattern();
}
const bool buttonState = !digitalRead(PIN_BUTTON_NEXT_PATTERN);
if (buttonState != lastButtonState)
{
if (buttonState == false)
{
selectNextPattern();
}
}
lastButtonState = buttonState;
showPattern();
delay(10);
}
//------------------------------------------------------------------------------
typedef void (*PatternFct)();
const PatternFct patternFunctions[] =
{
&displayRGB,
&displayRuler,
&displayAnimation,
&displayRainbow,
[]() { displayColor(CRGB{0x40, 0x00, 0x00}); }, // red
[]() { displayColor(CRGB{0x40, 0x40, 0x00}); }, // yellow
[]() { displayColor(CRGB{0x00, 0x40, 0x00}); }, // green
[]() { displayColor(CRGB{0x00, 0x40, 0x40}); }, // cyan
[]() { displayColor(CRGB{0x00, 0x00, 0x40}); }, // blue
[]() { displayColor(CRGB{0x40, 0x00, 0x40}); }, // magenta
[]() { displayColor(CRGB{0x40, 0x40, 0x40}); }, // white
[]() { displayColor(CRGB{0xFF, 0x00, 0x00}); }, // red
[]() { displayColor(CRGB{0xFF, 0xFF, 0x00}); }, // yellow
[]() { displayColor(CRGB{0x00, 0xFF, 0x00}); }, // green
[]() { displayColor(CRGB{0x00, 0xFF, 0xFF}); }, // cyan
[]() { displayColor(CRGB{0x00, 0x00, 0xFF}); }, // blue
[]() { displayColor(CRGB{0xFF, 0x00, 0xFF}); }, // magenta
[]() { displayColor(CRGB{0xFF, 0xFF, 0xFF}); }, // white
nullptr // end
};
//------------------------------------------------------------------------------
void selectNextPattern()
{
animationCounter = 0;
animationColorIndex = 0;
FastLED.clear();
if (patternFunctions[++patternIndex] == nullptr)
{
selectFirstPattern();
}
}
//------------------------------------------------------------------------------
void selectFirstPattern()
{
patternIndex = 0;
animationCounter = 0;
animationColorIndex = 0;
FastLED.clear();
}
//------------------------------------------------------------------------------
void showPattern()
{
patternFunctions[patternIndex]();
FastLED.show();
}
//------------------------------------------------------------------------------
void displayRuler()
{
FastLED.clear();
for (uint16_t i = 1; i <= NUM_LEDS; ++i)
{
// every 100th LED white
if (i % 100 == 0)
{
leds[i - 1] = CRGB{0x80, 0x80, 0x80};
}
// every 50th LED blue
else if (i % 50 == 0)
{
leds[i - 1] = CRGB{0, 0, 0x80};
}
// every 10th LED red
else if (i % 10 == 0)
{
leds[i - 1] = CRGB{0x20, 0, 0};
}
// every 5th LED magenta
else if (i % 5 == 0)
{
leds[i - 1] = CRGB{0x04, 0, 0x04};
}
// every 2nd (odd) LED green
else if (i % 2)
{
leds[i - 1] = CRGB{0, 0x04, 0};
}
}
}
//------------------------------------------------------------------------------
void displayAnimation()
{
static const CRGB colorTable[] =
{
CRGB{0x20, 0x00, 0x00}, // red
CRGB{0x20, 0x20, 0x00}, // yellow
CRGB{0x00, 0x20, 0x00}, // green
CRGB{0x00, 0x20, 0x20}, // cyan
CRGB{0x00, 0x00, 0x20}, // blue
CRGB{0x20, 0x00, 0x20}, // magenta
CRGB{0x20, 0x20, 0x20}, // white
CRGB{0x00, 0x00, 0x00} // black
};
leds[animationCounter] = colorTable[animationColorIndex];
if (++animationCounter >= NUM_LEDS)
{
animationCounter = 0;
if (++animationColorIndex >= ARRAYLEN(colorTable))
{
animationColorIndex = 0;
}
}
}
//------------------------------------------------------------------------------
void displayRGB()
{
static const CRGB colorTable[] =
{
CRGB{0x10, 0x00, 0x00}, // red
CRGB{0x00, 0x00, 0x00}, // black
CRGB{0x00, 0x10, 0x00}, // green
CRGB{0x00, 0x00, 0x00}, // black
CRGB{0x00, 0x00, 0x10}, // blue
CRGB{0x00, 0x00, 0x00} // black
};
for (uint16_t i = 0; i < NUM_LEDS; ++i)
{
const uint16_t colorIndex = (i / 5) % ARRAYLEN(colorTable);
const CRGB color = colorTable[colorIndex];
const uint16_t ledIndex = (i + animationCounter / 10) % NUM_LEDS;
leds[ledIndex] = color;
}
if (++animationCounter >= NUM_LEDS * 10)
{
animationCounter = 0;
}
}
//------------------------------------------------------------------------------
void displayRainbow()
{
static uint8_t hue = 0;
fill_rainbow(leds, NUM_LEDS, hue, 5);
EVERY_N_MILLISECONDS(20) { hue++; }
}
//------------------------------------------------------------------------------
void displayColor(const CRGB &color)
{
FastLED.clear();
for (uint16_t i = 0; i < NUM_LEDS; ++i)
{
leds[i] = color;
}
}
//------------------------------------------------------------------------------