-
Notifications
You must be signed in to change notification settings - Fork 33
/
NeoPixel_KnightRider.ino
139 lines (129 loc) · 6.27 KB
/
NeoPixel_KnightRider.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
// 888b 888 8888888b. d8b 888
// 8888b 888 888 Y88b Y8P 888
// 88888b 888 888 888 888
// 888Y88b 888 .d88b. .d88b. 888 d88P 888 888 888 .d88b. 888
// 888 Y88b888 d8P Y8b d88""88b 8888888P" 888 `Y8bd8P' d8P Y8b 888
// 888 Y88888 88888888 888 888 888 888 X88K 88888888 888
// 888 Y8888 Y8b. Y88..88P 888 888 .d8""8b. Y8b. 888
// 888 Y888 "Y8888 "Y88P" 888 888 888 888 "Y8888 888
//
// NeoPixel-KnightRider
//
// A highly configurable Knight Rider (larson display) routine for your NeoPixels
// (WS2812 RGB LED)
//-----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2013 Technobly - [email protected] - August 13th 2013
//
// 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.
//
//-----------------------------------------------------------------------------
// ASCII GEN http://patorjk.com/software/taag/#p=display&f=Colossal&t=NeoPixel
//
//-----------------------------------------------------------------------------
#include <Adafruit_NeoPixel.h>
// SETUP YOUR OUTPUT PIN AND NUMBER OF PIXELS
#define PIN A5
#define NUM_PIXELS 16
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
clearStrip(); // Initialize all pixels to 'off'
delay(1000);
}
void loop() {
knightRider(3, 32, 4, 0xFF1000); // Cycles, Speed, Width, RGB Color (original orange-red)
knightRider(3, 32, 3, 0xFF00FF); // Cycles, Speed, Width, RGB Color (purple)
knightRider(3, 32, 2, 0x0000FF); // Cycles, Speed, Width, RGB Color (blue)
knightRider(3, 32, 5, 0xFF0000); // Cycles, Speed, Width, RGB Color (red)
knightRider(3, 32, 6, 0x00FF00); // Cycles, Speed, Width, RGB Color (green)
knightRider(3, 32, 7, 0xFFFF00); // Cycles, Speed, Width, RGB Color (yellow)
knightRider(3, 32, 8, 0x00FFFF); // Cycles, Speed, Width, RGB Color (cyan)
knightRider(3, 32, 2, 0xFFFFFF); // Cycles, Speed, Width, RGB Color (white)
clearStrip();
delay(2000);
// Iterate through a whole rainbow of colors
for(byte j=0; j<252; j+=7) {
knightRider(1, 16, 2, colorWheel(j)); // Cycles, Speed, Width, RGB Color
}
clearStrip();
delay(2000);
}
// Cycles - one cycle is scanning through all pixels left then right (or right then left)
// Speed - how fast one cycle is (32 with 16 pixels is default KnightRider speed)
// Width - how wide the trail effect is on the fading out LEDs. The original display used
// light bulbs, so they have a persistance when turning off. This creates a trail.
// Effective range is 2 - 8, 4 is default for 16 pixels. Play with this.
// Color - 32-bit packed RGB color value. All pixels will be this color.
// knightRider(cycles, speed, width, color);
void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
uint32_t old_val[NUM_PIXELS]; // up to 256 lights!
// Larson time baby!
for(int i = 0; i < cycles; i++){
for (int count = 1; count<NUM_PIXELS; count++) {
strip.setPixelColor(count, color);
old_val[count] = color;
for(int x = count; x>0; x--) {
old_val[x-1] = dimColor(old_val[x-1], width);
strip.setPixelColor(x-1, old_val[x-1]);
}
strip.show();
delay(speed);
}
for (int count = NUM_PIXELS-1; count>=0; count--) {
strip.setPixelColor(count, color);
old_val[count] = color;
for(int x = count; x<=NUM_PIXELS ;x++) {
old_val[x-1] = dimColor(old_val[x-1], width);
strip.setPixelColor(x+1, old_val[x+1]);
}
strip.show();
delay(speed);
}
}
}
void clearStrip() {
for( int i = 0; i<NUM_PIXELS; i++){
strip.setPixelColor(i, 0x000000); strip.show();
}
}
uint32_t dimColor(uint32_t color, uint8_t width) {
return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
}
// Using a counter and for() loop, input a value 0 to 251 to get a color value.
// The colors transition like: red - org - ylw - grn - cyn - blue - vio - mag - back to red.
// Entering 255 will give you white, if you need it.
uint32_t colorWheel(byte WheelPos) {
byte state = WheelPos / 21;
switch(state) {
case 0: return strip.Color(255, 0, 255 - ((((WheelPos % 21) + 1) * 6) + 127)); break;
case 1: return strip.Color(255, ((WheelPos % 21) + 1) * 6, 0); break;
case 2: return strip.Color(255, (((WheelPos % 21) + 1) * 6) + 127, 0); break;
case 3: return strip.Color(255 - (((WheelPos % 21) + 1) * 6), 255, 0); break;
case 4: return strip.Color(255 - (((WheelPos % 21) + 1) * 6) + 127, 255, 0); break;
case 5: return strip.Color(0, 255, ((WheelPos % 21) + 1) * 6); break;
case 6: return strip.Color(0, 255, (((WheelPos % 21) + 1) * 6) + 127); break;
case 7: return strip.Color(0, 255 - (((WheelPos % 21) + 1) * 6), 255); break;
case 8: return strip.Color(0, 255 - ((((WheelPos % 21) + 1) * 6) + 127), 255); break;
case 9: return strip.Color(((WheelPos % 21) + 1) * 6, 0, 255); break;
case 10: return strip.Color((((WheelPos % 21) + 1) * 6) + 127, 0, 255); break;
case 11: return strip.Color(255, 0, 255 - (((WheelPos % 21) + 1) * 6)); break;
default: return strip.Color(0, 0, 0); break;
}
}