Skip to content

Instantly share code, notes, and snippets.

@mkral
Created January 31, 2018 00:38
Show Gist options
  • Select an option

  • Save mkral/b85de9422bdc1517c0e5acbb73989e55 to your computer and use it in GitHub Desktop.

Select an option

Save mkral/b85de9422bdc1517c0e5acbb73989e55 to your computer and use it in GitHub Desktop.
Arduino + Stranger Things
#include <Adafruit_NeoPixel.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(50, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Some example procedures showing how to display to the pixels:
delay(200);
spellMessage("happy birthday lily and natalie", false);
delay(200);
glowFlash();
delay(200);
spellMessage("run", true);
delay(200);
glowFlash();
}
void spellMessage(String message, bool stayLit) {
for(int i=0; i<message.length(); i++) {
char letter = message[i];
if (letter == ' ') {
delay(500);
continue;
}
int lightIndex = indexForChar(letter);
strip.setPixelColor(lightIndex, strip.Color(255,255,255));
strip.show();
delay(500);
strip.setPixelColor(lightIndex, strip.Color(0,0,0));
strip.show();
delay(1000);
}
if (stayLit) {
for(int i=0; i<message.length(); i++) {
char letter = message[i];
int lightIndex = indexForChar(letter);
strip.setPixelColor(lightIndex, strip.Color(255,255,255));
}
strip.show();
delay(1000);
colorWipe(strip.Color(0,0,0), 0);
delay(200);
}
}
void glow(int startAlpha, int endAlpha, int duration) {
colorWipe(strip.Color(startAlpha,startAlpha,startAlpha), 0);
int difference = endAlpha - startAlpha;
int delayT = duration / difference;
for(int i=0; i < delayT; i++) {
int newAlpha = (i * delayT) + startAlpha;
colorWipe(strip.Color(newAlpha,newAlpha,newAlpha), 0);
delay(delayT);
}
}
void glowFlash() {
int alphaGradient[] = {0, 10, 50, 25, 125, 255, 120, 60, 15, 0};
colorWipe(strip.Color(0,0,0), 0);
for (int i=0; i < sizeof(alphaGradient); i++) {
int alpha = alphaGradient[i];
colorWipe(strip.Color(alpha,alpha,alpha), 0);
delay(100);
}
colorWipe(strip.Color(0,0,0), 0);
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
delay(wait);
}
strip.show();
}
int indexForChar(char c) {
switch (c) {
case 'a':
return 0;
case 'b':
return 1;
case 'c':
return 2;
case 'd':
return 3;
case 'e':
return 4;
case 'f':
return 5;
case 'g':
return 6;
case 'h':
return 7;
case 'i':
return 16;
case 'j':
return 15;
case 'k':
return 14;
case 'l':
return 13;
case 'm':
return 12;
case 'n':
return 11;
case 'o':
return 10;
case 'p':
return 9;
case 'q':
return 8;
case 'r':
return 17;
case 's':
return 18;
case 't':
return 19;
case 'u':
return 20;
case 'v':
return 21;
case 'w':
return 22;
case 'x':
return 23;
case 'y':
return 24;
case 'z':
return 25;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment