Last active
June 26, 2017 19:37
-
-
Save PaulWieland/d2fdd3cab9c09b32abe476ae2f8f1315 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include "FastLED.h" | |
class Vanity | |
{ | |
public: | |
Vanity(CRGB *leds, int NUM_LEDS){ | |
this->num_leds = NUM_LEDS; | |
this->_state = false; | |
this->_leds = leds; | |
this->currentPatternNumber = 0; | |
this->patternCount = 4; | |
} // constructor | |
int num_leds; | |
uint8_t currentPatternNumber; | |
uint8_t patternCount; | |
bool allOn(){ | |
for(uint8_t i = 0; i < this->num_leds - 1; i++){ | |
if(this->_leds[i].getLuma() < 255){ | |
// Serial.println("Not lit" + (String) i); | |
return false; | |
} | |
} | |
// Serial.println("ALL LIT"); | |
return true; | |
} | |
void on(){ | |
static uint8_t i = 0; | |
static int lite = 0; | |
static int lastLit = 0; | |
// If the Vanity is Off, start at position 0 | |
if(!this->_state){ | |
i = 0; | |
} | |
this->_state = true; | |
// Stop once the last LED is lit | |
if(this->allOn()){ | |
return; | |
} | |
// Loop counter, resets to zero after 255 | |
i++; | |
// do an animation to turn on the LEDs Depending on the current pattern number, | |
switch(this->currentPatternNumber){ | |
case 0: // Wrap around perimiter with colored leading dot using ease in & out | |
EVERY_N_MILLISECONDS(60){ | |
this->_hue++; | |
} | |
EVERY_N_MILLISECONDS(5) { // Non blocking delay to control the animation speed | |
Serial.println((String) i + " :: " + (String) ease8InOutQuad(i)); | |
// lite = the led to be lit up | |
// ease8InOutQuad = pass a value from 0-255 and receive back a value representing an eased curve | |
// lerp8by8 = map the passed in value between two set points | |
lite = lerp8by8(0, this->num_leds, ease8InOutQuad(i)); | |
// The lerped easing will cause LEDs to be skipped, so we fill from the last lit LED to the current lit LED | |
fill_solid(this->_leds+lastLit, lite - lastLit, CRGB::White); | |
// Make the lead lite a color, but skip the last LED since the animation stops once the last light is lit | |
if(lite < this->num_leds - 4){ | |
this->_leds[lite + 1] = CHSV( this->_hue, 255, 192); | |
this->_leds[lite + 2] = CHSV( this->_hue, 255, 192); | |
this->_leds[lite + 3] = CHSV( this->_hue, 255, 192); | |
} | |
// Serial.println("0 lit " + (String) lastLit + "::" + (String) lite + "::" + (String) (lite - lastLit)); | |
lastLit = lite; | |
} | |
break; | |
case 1: // Grow from one corner and meet at the opposite corner | |
EVERY_N_MILLISECONDS(20) { // Non blocking delay to control the animation speed | |
lite = lerp8by8(0, this->num_leds / 2, ease8InOutQuad(i)); | |
Serial.println("1 lit " + (String) lite + ":::" + (String) (this->num_leds - lite)); | |
fill_solid(this->_leds+lastLit, lite - lastLit, CRGB::White); | |
// this->_leds[lite] = CRGB::White; | |
fill_solid(this->_leds+(this->num_leds - lite), lite - lastLit, CRGB::White); | |
// this->_leds[this->num_leds - 1 - lite] = CRGB::White; | |
lastLit = lite; | |
} | |
break; | |
case 2: // Corners shooting out | |
EVERY_N_MILLISECONDS(10) { // Non blocking delay to control the animation speed | |
lite = lerp8by8(0, this->num_leds / 4, ease8InOutQuad(i)); | |
Serial.println("2 lite " + (String) (((this->num_leds / 4) * 3) + lite)); | |
this->_leds[(this->num_leds / 4) + lite] = CRGB::White; | |
this->_leds[(this->num_leds / 4) - lite] = CRGB::White; | |
this->_leds[((this->num_leds / 4) * 3) + (lite - 1)] = CRGB::White; | |
this->_leds[((this->num_leds / 4) * 3) - lite] = CRGB::White; | |
} | |
break; | |
case 3: // Random blocks lighting up | |
Serial.println("Rand: " + (String) random16(this->num_leds-4)); | |
fill_solid(this->_leds+random16(this->num_leds-3), 4, CRGB::White); | |
break; | |
} | |
} // on() | |
void off(){ | |
if(this->_state){ | |
this->nextPattern(); | |
} | |
this->_state = false; | |
fadeToBlackBy(this->_leds, this->num_leds, 10); | |
} // off() | |
void nextPattern(){ | |
// add one to the current pattern number, and wrap around at the end | |
this->currentPatternNumber = (this->currentPatternNumber + 1) % this->patternCount; | |
// Serial.println(this->currentPatternNumber); | |
} | |
private: | |
bool _state; | |
CRGB *_leds; | |
uint8_t _currentPatternNumber = 0; // Index number of which pattern is current | |
uint8_t _hue = 0; // rotating "base color" used by many of the patterns | |
}; | |
// V = Vanity | |
// N = Nightlight | |
#define LED_TYPE WS2811 | |
#define COLOR_ORDER GRB | |
#define V_TRIGGER_PIN 6 | |
#define N_TRIGGER_PIN 5 | |
#define V_DATA_PIN 12 | |
#define N_DATA_PIN 11 | |
#define V_NUM_LEDS 216 | |
#define N_NUM_LEDS 58 | |
CRGB V_leds[V_NUM_LEDS]; | |
CRGB N_leds[N_NUM_LEDS]; | |
#define BRIGHTNESS 255 | |
#define FRAMES_PER_SECOND 240 | |
Vanity Vanity(V_leds, V_NUM_LEDS); | |
void setup() { | |
Serial.begin(115200); | |
delay(3000); // 3 second delay for recovery | |
// The pins the Insteon I/O Lincs relays will ground out when activated | |
pinMode(V_TRIGGER_PIN, INPUT_PULLUP); | |
pinMode(N_TRIGGER_PIN, INPUT_PULLUP); | |
// tell FastLED about the LED strip configuration | |
FastLED.addLeds<LED_TYPE,V_DATA_PIN,COLOR_ORDER>(V_leds, V_NUM_LEDS).setCorrection(TypicalLEDStrip).setTemperature(Tungsten100W); | |
FastLED.addLeds<LED_TYPE,N_DATA_PIN,COLOR_ORDER>(N_leds, N_NUM_LEDS).setCorrection(TypicalLEDStrip); | |
// set master brightness control | |
// How to control the brightness of the different strands individually?? | |
FastLED.setBrightness(BRIGHTNESS); | |
FastLED.clear(); | |
// Vanity.nextPattern(); | |
// Vanity.nextPattern(); | |
} | |
void loop() | |
{ | |
// Vanity.on(); | |
if(digitalRead(V_TRIGGER_PIN) == LOW){ | |
Vanity.on(); | |
}else{ | |
Vanity.off(); | |
} | |
// nightLightsOn(); | |
// nightLightsOff(); | |
FastLED.show(); | |
// FastLED.delay(1000/FRAMES_PER_SECOND); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment