Files
@ 6255b67a731b
Branch filter:
Location: Wearables/Neopixel-Goggles/Neopixel_Goggles.ino
6255b67a731b
7.9 KiB
text/x-arduino
Add config options
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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | #include <Adafruit_NeoPixel.h>
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
#include <avr/power.h>
#endif
/* Begin config section
-----------------------*/
#define PIN 0
#define BRIGHTNESS 64
#define MODE_CHANGE_TIME 10000
#define SLOW_INTERVAL_MIN 25
#define SLOW_INTERVAL_MAX 55
#define COLOR_MIN 0
#define COLOR_MAX 255
#define STEPS_MIN 5
#define STEPS_MAX 10
#define RAINBOW_CYCLE_MIN_INTERVAL 1
#define RAINBOW_CYCLE_MAX_INTERVAL 10
#define BLINK_MIN_INTERVAL 250
#define BLINK_MAX_INTERVAL 500
/* End config section */
enum pattern {
RAINBOW_CYCLE,
COLOR_WIPE,
DOUBLE_COLOR_WIPE,
SCANNER,
DOUBLE_SCANNER,
RANDOM,
DOUBLE_RANDOM,
BLINK,
DOT
};
class NeoPatterns : public Adafruit_NeoPixel {
public:
pattern ActivePattern;
uint16_t Interval;
unsigned long lastUpdate;
uint32_t Color1;
uint16_t TotalSteps;
uint16_t Index;
void (*OnComplete)();
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)()) : Adafruit_NeoPixel(pixels, pin, type) {
OnComplete = callback;
}
void Update() {
if ((millis() - lastUpdate) > Interval) {
lastUpdate = millis();
switch (ActivePattern) {
case RAINBOW_CYCLE:
RainbowCycleUpdate();
break;
case COLOR_WIPE:
ColorWipeUpdate();
break;
case DOUBLE_COLOR_WIPE:
DoubleColorWipeUpdate();
break;
case SCANNER:
ScannerUpdate();
break;
case DOUBLE_SCANNER:
DoubleScannerUpdate();
break;
case RANDOM:
RandomUpdate();
break;
case DOUBLE_RANDOM:
DoubleRandomUpdate();
break;
case BLINK:
BlinkUpdate();
break;
case DOT:
DotUpdate();
break;
default:
break;
}
}
}
void Increment() {
show();
Index++;
if (Index >= TotalSteps) {
Index = 0;
OnComplete();
}
}
void RainbowCycle(uint16_t interval) {
ActivePattern = RAINBOW_CYCLE;
Interval = interval;
TotalSteps = 255;
Index = 0;
}
void RainbowCycleUpdate() {
for (int i = 0; i < numPixels(); i++) {
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
}
Increment();
}
void ColorWipe(uint32_t color, uint16_t interval) {
ActivePattern = COLOR_WIPE;
Interval = interval;
TotalSteps = numPixels();
Color1 = color;
Index = 0;
}
void ColorWipeUpdate() {
setPixelColor(Index, Color1);
Increment();
}
void DoubleColorWipe(uint32_t color, uint16_t interval) {
ActivePattern = DOUBLE_COLOR_WIPE;
Interval = interval;
TotalSteps = numPixels() / 2;
Color1 = color;
Index = 0;
}
void DoubleColorWipeUpdate() {
setPixelColor(Index, Color1);
setPixelColor(numPixels() - 1 - Index, Color1);
Increment();
}
void Scanner(uint32_t color1, uint16_t interval) {
ActivePattern = SCANNER;
Interval = interval;
TotalSteps = (numPixels() - 1) * 2;
Color1 = color1;
Index = 0;
}
void ScannerUpdate() {
for (int i = 0; i < numPixels(); i++) {
if (i == Index) {
setPixelColor(i, Color1);
}
else if (i == TotalSteps - Index) {
setPixelColor(i, Color1);
}
else {
setPixelColor(i, DimColor(getPixelColor(i)));
}
}
Increment();
}
void DoubleScanner(uint32_t color1, uint16_t interval) {
ActivePattern = DOUBLE_SCANNER;
Interval = interval;
TotalSteps = (numPixels() / 2);
Color1 = color1;
Index = 0;
}
void DoubleScannerUpdate() {
for (int i = 0; i < numPixels() / 2; i++) {
if (i == Index) {
setPixelColor(i, Color1); // First Eye
setPixelColor(numPixels() - 1 - i, Color1); // Second Eye
}
else {
setPixelColor(i, DimColor(getPixelColor(i)));
setPixelColor(numPixels() - 1 - i, DimColor(getPixelColor(i)));
}
}
Increment();
}
void Random(uint32_t color1, uint16_t steps, uint16_t interval) {
ActivePattern = RANDOM;
Interval = interval;
TotalSteps = steps;
Color1 = color1;
Index = 0;
}
void RandomUpdate() {
setPixelColor(random(numPixels()), Color1);
Increment();
}
void DoubleRandom(uint32_t color1, uint16_t steps, uint16_t interval) {
ActivePattern = DOUBLE_RANDOM;
Interval = interval;
TotalSteps = steps;
Color1 = color1;
Index = 0;
}
void DoubleRandomUpdate() {
int i = random(numPixels() / 2);
setPixelColor(i, Color1);
setPixelColor(numPixels() - 1 - i, Color1);
Increment();
}
void Blink(uint32_t color1, uint16_t steps, uint16_t interval) {
ActivePattern = BLINK;
Interval = interval;
if (steps % 2 != 0) {
steps++;
}
TotalSteps = steps;
Color1 = color1;
Index = 0;
}
void BlinkUpdate() {
uint32_t c;
if (Index % 2 == 0) {
c = Color1;
} else {
c = Color(0, 0, 0);
}
ColorSet(c);
Increment();
}
void Dot(uint32_t color1, uint16_t steps, uint16_t interval) {
ActivePattern = DOT;
Interval = interval;
TotalSteps = steps;
Color1 = color1;
Index = 0;
}
void DotUpdate() {
ColorSet(Color(0, 0, 0));
show();
setPixelColor(random(numPixels()), Color1);
Increment();
}
uint32_t DimColor(uint32_t color) {
return Color(((color >> 16) & 0xFF) >> 1, ((color >> 8) & 0xFF) >> 1, (color & 0xFF) >> 1);
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
else {
WheelPos -= 170;
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
void ColorSet(uint32_t color) {
for (int i = 0; i < numPixels(); i++) {
setPixelColor(i, color);
}
}
};
void RingsComplete();
NeoPatterns Rings(32, PIN, NEO_GRB + NEO_KHZ800, &RingsComplete);
uint8_t mode = 0;
uint32_t prev_time;
void setup() {
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
Rings.begin();
Rings.setBrightness(BRIGHTNESS);
Rings.RainbowCycle(random(RAINBOW_CYCLE_MIN_INTERVAL, RAINBOW_CYCLE_MAX_INTERVAL + 1));
prev_time = millis();
}
void loop() {
Rings.Update();
uint32_t t = millis();
if ((t - prev_time) >= MODE_CHANGE_TIME) {
mode = random(0, 9);
prev_time = t;
}
}
void RingsComplete()
{
uint16_t slow_interval = random(SLOW_INTERVAL_MIN, SLOW_INTERVAL_MAX + 1);
//uint16_t fast_interval = random(1, 11);
uint32_t color = Rings.Wheel(random(COLOR_MIN, COLOR_MAX + 1));
uint16_t steps = random(STEPS_MIN, STEPS_MAX + 1);
switch (mode) {
case 0:
Rings.RainbowCycle(random(RAINBOW_CYCLE_MIN_INTERVAL, RAINBOW_CYCLE_MAX_INTERVAL + 1));
break;
case 1:
Rings.ColorWipe(color, slow_interval);
break;
case 2:
Rings.DoubleColorWipe(color, slow_interval);
break;
case 3:
Rings.Scanner(color, slow_interval);
break;
case 4:
Rings.DoubleScanner(color, slow_interval);
break;
case 5:
Rings.Random(color, steps, slow_interval);
break;
case 6:
Rings.DoubleRandom(color, steps, slow_interval);
break;
case 7:
Rings.Blink(color, steps, random(BLINK_MIN_INTERVAL, BLINK_MAX_INTERVAL + 1));
break;
case 8:
Rings.Dot(color, steps, slow_interval);
break;
default:
break;
}
}
|