Skip to content

Instantly share code, notes, and snippets.

@perigalacticon
Last active November 5, 2017 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perigalacticon/deabf497209566eed3a949c7c56b52d7 to your computer and use it in GitHub Desktop.
Save perigalacticon/deabf497209566eed3a949c7c56b52d7 to your computer and use it in GitHub Desktop.
tweaking4All_creepy_halloween_eyes WS2812B Led Strip Effect
// Based On: https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#halloween_eyes
// Modified by: Perigalacticon 11/5/2017.
// Includes a button input that triggers a simple explosion effect.
// Colors values are hand tuned for my LED Strip which is significantly more red than it should be.
//#include <Adafruit_NeoPixel.h>
#include "FastLED.h"
#define NUM_LEDS 50
CRGB leds[NUM_LEDS];
#define LED_PIN 9
#define BUTTON_PIN 10
#define EXPLOSION_PIN 11
// RGB struct
typedef struct {
byte r;
byte g;
byte b;
} color;
byte bgLevel = 30;
byte bgSat = 255;
byte bgHue = 10;
color green = {0, 255, 0};
color red = {255, 0, 0 };
color blue = {0, 0, 255};
color orange = {255, 70, 0};
color yellow = {254, 254, 34};
color purple = {158, 8, 148};
long lastBackgroundUpdateTime;
long updateBackgroundInterval = 20;
color eyeColors[] = {green, purple, yellow, red, orange, blue};
int numberOfEyeColors = 6; // sizeof(eyeColors)/sizeof(color);
int maxFlickers = 100;
int flickerDelay = 80;
boolean redFlash = false;
byte flashR;
byte flashG;
byte flashB;
int numberOfBlinks;
int offPause;
//===============================================================================================
void setup()
{
Serial.begin(115200);
FastLED.addLeds<WS2811, LED_PIN, RGB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
// GRB for WS2812B strips, RGB for outdoor WS2811.
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(EXPLOSION_PIN, OUTPUT);
randomSeed(analogRead(A2));
digitalWrite(EXPLOSION_PIN, HIGH);
}
//===============================================================================================
void loop()
{
if (digitalRead(BUTTON_PIN) == LOW)
{
runExplosion();
}
else
{
// Uncomment to set background color by potentiometers:
// bgLevel = analogRead(A0) / 4;
// bgSat = analogRead(A1) / 4;
// bgHue = analogRead(A5) / 4;
//
// Serial.print(bgLevel);
// Serial.print("\t");
// Serial.print(bgSat);
// Serial.print("\t");
// Serial.println(bgHue);
int eyeColor = random(0, numberOfEyeColors);
int eyeR = eyeColors[eyeColor].r;
int eyeG = eyeColors[eyeColor].g;
int eyeB = eyeColors[eyeColor].b;
HalloweenEyes(eyeR, eyeG, eyeB,
random(1, 3), random(1, 6),
random(0, 2), random(5, 40), random(10, 40),
random(200, 400));
}
}
//===============================================================================================
void HalloweenEyes(byte red, byte green, byte blue,
int EyeWidth, int EyeSpace,
boolean Fade, int Steps, int FadeDelay,
int OnPause)
{
int i;
int j;
int k;
int StartPoint = random( 0, NUM_LEDS - (2 * EyeWidth) - EyeSpace );
int Start2ndEye = StartPoint + EyeWidth + EyeSpace;
// draw eyes:
// Serial.println("draw eyes");
for (i = 0; i < EyeWidth; i++) {
setPixel(StartPoint + i, red, green, blue);
setPixel(Start2ndEye + i, red, green, blue);
}
showStrip();
if (Fade == true)
{
// Serial.println("start fade");
delay(OnPause);
float r, g, b;
for (j = Steps; j >= 0; j--)
{
r = j * (red / Steps);
g = j * (green / Steps);
b = j * (blue / Steps);
for (i = 0; i < EyeWidth; i++) {
setPixel(StartPoint + i, r, g, b);
setPixel(Start2ndEye + i, r, g, b);
}
showStrip();
delay(FadeDelay);
}
// Serial.println("fade done");
}
else
{
numberOfBlinks = random(0, 3);
// Serial.print("# of blinks = ");
// Serial.println(numberOfBlinks);
for (k = 0; k < numberOfBlinks; k++)
{
offPause = random(50, 150); // different off time each blink
// Serial.println("on delay1");
delay(OnPause);
// reset to background color:
// Serial.println("eyes off, fill solid:");
fill_solid(leds, NUM_LEDS, CHSV(bgHue, bgSat, bgLevel));
FastLED.show();
// Serial.println("off delay");
delay(offPause);
// draw eyes again:
for (i = 0; i < EyeWidth; i++) {
setPixel(StartPoint + i, red, green, blue);
setPixel(Start2ndEye + i, red, green, blue);
}
showStrip();
// Serial.println("on delay2");
delay(OnPause);
}
}
// draw background:
// Serial.println("draw background");
fill_solid(leds, NUM_LEDS, CHSV(bgHue, bgSat, bgLevel)); // reset to background color
FastLED.show();
if (millis() - lastBackgroundUpdateTime >= updateBackgroundInterval)
{
bgHue = bgHue + 25;
Serial.print("new bgHue1 = ");
Serial.println(bgHue);
lastBackgroundUpdateTime = millis();
}
}
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
void runExplosion(void)
{
// forward the explosion signal to other device:
digitalWrite(EXPLOSION_PIN, LOW);
// fast flicker
Serial.println("explosion");
for (int j = 0; j < maxFlickers; j++)
{
redFlash = !redFlash;
if (redFlash) {
flashR = 255;
flashG = 0;
flashB = 0;
}
else
{
flashR = 78;
flashG = 255;
flashB = 101;
}
fill_solid(leds, NUM_LEDS, CRGB(flashR, flashG, flashB)); // reset to background color
FastLED.show();
delay(flickerDelay / 2);
fill_solid(leds, NUM_LEDS, CRGB(flashR, flashG, flashB)); // reset to background color
FastLED.show();
delay(flickerDelay);
}
digitalWrite(EXPLOSION_PIN, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment