Last active
December 18, 2017 23:44
-
-
Save steveosoule/f9d98b58ae666bce6a6752cefa2d11aa to your computer and use it in GitHub Desktop.
Stranger Things Arduino Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/steveosoule/f9d98b58ae666bce6a6752cefa2d11aa | |
// https://www.arduino.cc/reference/en/ | |
// http://www.instructables.com/id/Arduino-Based-Stranger-Things-Lights/ | |
// https://github.com/FastLED/FastLED | |
/** | |
* GLOBAL CONFIGURATION | |
**/ | |
#include <FastLED.h> | |
#define COLOR_ORDER RGB | |
#define DATA_PIN 6 // this is the data pin connected to the LED strip. If using WS2801 you also need a clock pin | |
#define NUM_LEDS 100 // change this for the number of LEDs in the strip | |
// Control the Speed & Delays | |
#define SPEED_RATE 5 // 1=FAST/DEV/TEST, 5=NORMAL, 10=SLOW | |
#define DURATION_XSMALL SPEED_RATE * 1 | |
#define DURATION_SMALL SPEED_RATE * 10 | |
#define DURATION_MEDIUM SPEED_RATE * 100 | |
#define DURATION_LARGE SPEED_RATE * 1000 | |
#define DURATION_XLARGE SPEED_RATE * 10000 | |
#define DURATION_CHAR DURATION_MEDIUM | |
#define DURATION_SPACE DURATION_MEDIUM * 3 | |
#define DURATION_MESSAGE DURATION_LARGE | |
#define RANDOM_VARIANCE 0 // Decimal in range 0-1 | |
#define ALPHABET_CHAR_COUNT 26 | |
#define BYTE_ALPHA_OFFSET 97; // byte("a") = 97 | |
#define GLOW_MIN 0 // Integer 0-255 for RGB value | |
#define GLOW_STEP_RGB DURATION_XSMALL | |
#define GLOW_STEP_DELAY DURATION_XSMALL | |
CRGB leds[NUM_LEDS]; | |
/** | |
* ARDUINO: ONE-TIME SETUP | |
**/ | |
void setup() { | |
FastLED.addLeds < WS2811, DATA_PIN, COLOR_ORDER > (leds, NUM_LEDS); | |
Serial.begin(9600); | |
Serial.println("Setting up..."); | |
FastLED.clear(); | |
} | |
/** | |
* ARDUINO: CONTINUALLY CALLED LOOP/PLAY | |
*/ | |
void loop() { | |
play(); | |
} | |
/** | |
* CUSTOM: MAIN | |
*/ | |
void play() | |
{ | |
// light_message("abcdefghijklmnopqrstuvwxyz"); | |
light_message("Jingle Bells"); | |
scale_candy_cane(); | |
light_message("PIZZA"); | |
scale_candy_cane(); | |
light_message("Merry Christmas"); | |
scale(); | |
light_message("Miva"); | |
scale_candy_cane(); | |
light_message("Happy Holidays"); | |
scale(); | |
light_message("Happy Hanukkah"); | |
scale_candy_cane(); | |
glow_red(); | |
scale(); | |
light_message("I'm Here"); | |
scale(); | |
light_message("Run"); | |
scale_candy_cane(); | |
} | |
/** | |
* CUSTOM: HELPER FUNCTIONS | |
*/ | |
void light_message(String message) { | |
message.toLowerCase(); | |
for(int i = 0; i < message.length(); i++) { | |
char letter = message[i]; | |
if( isSpace(letter) ) { | |
FastLED.clear(); | |
FastLED.show(); | |
random_delay(DURATION_SPACE); | |
continue; | |
} | |
if( !isAlpha(letter) ) continue; | |
random_delay(DURATION_CHAR); | |
FastLED.clear(); | |
int letter_led_index = get_letter_led_index(letter); | |
set_led_random_color(letter_led_index); | |
FastLED.show(); | |
random_delay(DURATION_CHAR); | |
} | |
FastLED.clear(); | |
FastLED.show(); | |
random_delay(DURATION_MESSAGE); | |
} | |
int get_letter_led_index(char letter) { | |
/* | |
float letter_index = byte(letter) - BYTE_ALPHA_OFFSET; | |
float led_index = floor((letter_index / ALPHABET_CHAR_COUNT) * 100); | |
return led_index; | |
*/ | |
int led_index = 0; | |
if (letter == 'a') { led_index = 57; } | |
else if (letter == 'b') { led_index = 60; } | |
else if (letter == 'c') { led_index = 63; } | |
else if (letter == 'd') { led_index = 66; } | |
else if (letter == 'e') { led_index = 69; } | |
else if (letter == 'f') { led_index = 72; } | |
else if (letter == 'g') { led_index = 75; } | |
else if (letter == 'h') { led_index = 77; } | |
else if (letter == 'i') { led_index = 81; } | |
else if (letter == 'j') { led_index = 84; } | |
else if (letter == 'k') { led_index = 86; } | |
else if (letter == 'l') { led_index = 89; } | |
else if (letter == 'm') { led_index = 45; } | |
else if (letter == 'n') { led_index = 43; } | |
else if (letter == 'o') { led_index = 41; } | |
else if (letter == 'p') { led_index = 39; } | |
else if (letter == 'q') { led_index = 37; } | |
else if (letter == 'r') { led_index = 35; } | |
else if (letter == 's') { led_index = 33; } | |
else if (letter == 't') { led_index = 30; } | |
else if (letter == 'u') { led_index = 28; } | |
else if (letter == 'v') { led_index = 26; } | |
else if (letter == 'w') { led_index = 23; } | |
else if (letter == 'x') { led_index = 20; } | |
else if (letter == 'y') { led_index = 17; } | |
else if (letter == 'z') { led_index = 14; } | |
return led_index; | |
} | |
void random_delay(int duration) { | |
float random_low = (1-RANDOM_VARIANCE) * duration; | |
float random_high = (1+RANDOM_VARIANCE) * duration; | |
delay( random(random_low, random_high) ); | |
} | |
/** | |
* CUSTOM: SECONDARY ANIMATIONS | |
*/ | |
void scale() { | |
// for (int i = 0; i < NUM_LEDS; i++) { | |
for (int i = NUM_LEDS-1; i >= 0; i--) { | |
// FastLED.clear(); | |
delay(DURATION_SMALL); | |
set_led_random_color(i); | |
FastLED.show(); | |
} | |
delay(DURATION_MEDIUM); | |
FastLED.clear(); | |
} | |
void scale_candy_cane() { | |
for (int i = 0; i < NUM_LEDS; i++) { | |
if( i % 2 == 0 ) { | |
set_led_red(i); | |
} else { | |
set_led_white(i); | |
} | |
FastLED.show(); | |
} | |
FastLED.clear(); | |
} | |
void glow_red(){ | |
glow_red_up(); | |
glow_red_down(); | |
glow_red_up(); | |
glow_red_down(); | |
} | |
void glow_red_up() { | |
FastLED.clear(); | |
for( int i = GLOW_MIN; i < 255; i = i + GLOW_STEP_RGB ) { | |
int r = i; | |
int b = 0; | |
int g = 0; | |
for(int led_index = 0; led_index < NUM_LEDS; led_index++){ | |
leds[led_index] = CRGB(r,g,b); | |
} | |
FastLED.show(); | |
delay(GLOW_STEP_DELAY); | |
} | |
random_delay(DURATION_MESSAGE); | |
} | |
void glow_red_down() { | |
for (int i = 255; i > GLOW_MIN; i = i - GLOW_STEP_RGB){ | |
int r = i; | |
int b = 0; | |
int g = 0; | |
for(int led_index = 0; led_index < NUM_LEDS; led_index++){ | |
leds[led_index] = CRGB(r,g,b); | |
} | |
FastLED.show(); | |
delay(GLOW_STEP_DELAY); | |
} | |
random_delay(DURATION_MESSAGE); | |
} | |
/** | |
* CUSTOM: LED COLORS | |
*/ | |
void set_led_random_color(int led_index) { | |
leds[led_index] = CRGB(random(0,255),random(0,255),random(0,255)); | |
} | |
void set_led_white(int led_index) { | |
leds[led_index] = CRGB(255,255,255); | |
} | |
void set_led_red(int led_index) { | |
leds[led_index] = CRGB(255,0,0); | |
} | |
void set_led_green(int led_index) { | |
leds[led_index] = CRGB(0,255,0); | |
} | |
void set_led_blue(int led_index){ | |
leds[led_index] = CRGB(0,0,255); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment