Skip to content

Instantly share code, notes, and snippets.

@ctkjedi
Created September 1, 2018 09:13
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 ctkjedi/27c92abcb6b2d3672cfa5e311236574e to your computer and use it in GitHub Desktop.
Save ctkjedi/27c92abcb6b2d3672cfa5e311236574e to your computer and use it in GitHub Desktop.
outside lights particle
// This #include statement was automatically added by the Particle IDE.
#include <FastLED.h>
#include "Particle.h"
FASTLED_USING_NAMESPACE;
#define LED_PIN 4
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define NUM_LEDS 301 //88 for testing, 301 for prodction
#define BRIGHTNESS 50
#define FRAMES_PER_SECOND 30
bool gReverseDirection = false;
unsigned long previousMillis = 0;
unsigned long currentMillis;
unsigned long twinkleMillis = 0;
int eepValue;
int eepAddr = 2;
int mode;
int offset = 0;
int maxShells = round(NUM_LEDS / 30);
int lifespan = 30;
long totalModes = 5;
int changeToColor(String newColor);
uint32_t currentColor=0x000000;
uint32_t prevColor = 0x000000;
NSFastLED::CRGB leds[NUM_LEDS];
CRGBPalette16 gPal;
void runColor(String currentColor);
void runRainbow();
void twinkle();
void fireworks();
void newCane();
void runPalette(NSFastLED::CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette);
class FireworkShell {
int stringPos; //origin of the blast
int age; //how long the blast has been on the string
int maxLife; //how old this one can get
uint8_t hue; //color of the shell
uint8_t sat;
uint8_t val;
public:
FireworkShell(int xPos, int howOld, int deathAge, uint8_t shellColor, uint8_t shellSat, uint8_t shellVal) {
stringPos = xPos;
age = howOld;
maxLife = deathAge;
hue = shellColor;
sat = shellSat;
val = shellVal;
}
void Update() {
if (age < maxLife) {
int newPosR = stringPos + age;
int newPosL = stringPos - age;
if (newPosR < NUM_LEDS) leds[newPosR] += CHSV(hue, sat, val);
if (newPosL >= 0) leds[newPosL] += CHSV(hue, sat, val);
if (age > 0) {
int tailR = newPosR - 1;
int tailL = newPosL + 1;
if (tailR > 0 && tailR < NUM_LEDS) {
leds[tailR] = CHSV(hue,sat, random(50, 200));
}
if (tailL > 0 && tailL < NUM_LEDS) {
leds[tailL] = CHSV(hue,sat, random(50, 200));
}
}
}
if (age < maxLife + 20) {
age++;
} else {
age = 0;
hue = random(192, 255);
maxLife = random(5, lifespan);
stringPos = random(0, NUM_LEDS);
//Serial.println(maxLife);
}
}
};
FireworkShell shells[] = {
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 0), random(0,255), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(90, 110), random(0,255), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 0), random(0,0), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 0), random(0,255), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(90, 110), random(0,255), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 0), random(0,0), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 0), random(0,255), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(90, 110), random(0,255), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 0), random(0,0), random(0,255)),
FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255), random(0,255), random(0,255)),
//FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255), random(0,255), random(0,255)),
//FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255), random(0,255), random(0,255)),
//FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255), random(0,255), random(0,255)),
//FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255), random(0,255), random(0,255)),
//FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255), random(0,255), random(0,255)),
//FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255), random(0,255), random(0,255)),
};
void setup() {
delay(3000); // sanity delay
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
//FastLED.setMaxPowerInVoltsAndMilliamps(5, 1500);
randomSeed(analogRead(0));
//Serial.begin(9600);
Particle.variable("mode",mode);
Particle.function("updateMode", modeChange);
Particle.function("changeColor", changeToColor);
Particle.variable("newColor", currentColor);
EEPROM.get(eepAddr, eepValue);
if(eepValue == 0xFFFF) {
// EEPROM was empty -> initialize value
mode = 0;
}else{
mode = eepValue;
}
//mode = 0;
}
extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
void loop()
{
currentMillis = millis();
//Particle.publish("Mode is ", String (mode));
switch (mode) {
case 0:
runRainbow();
twinkle();
break;
case 1:
runPalette(leds, NUM_LEDS, gGradientPalettes[0]);
twinkle();
break;
case 2:
newCane();
//runPalette(leds, NUM_LEDS, gGradientPalettes[1]);
twinkle();
break;
case 3:
runPalette(leds, NUM_LEDS, gGradientPalettes[2]);
twinkle();
break;
case 4:
runPalette(leds, NUM_LEDS, gGradientPalettes[3]);
twinkle();
break;
case 5:
runPalette(leds, NUM_LEDS, gGradientPalettes[4]);
twinkle();
break;
case 6:
fireworks();
break;
case 7:
runPalette(leds, NUM_LEDS, gGradientPalettes[5]);
twinkle();
case 8:
runPalette(leds, NUM_LEDS, gGradientPalettes[6]);
twinkle();
case 9:
runPalette(leds, NUM_LEDS, gGradientPalettes[7]);
twinkle();
case 10:
runColor(currentColor);
twinkle();
break;
}
}
/* int changeToColor(String newColor){
Particle.publish("Color received is ", String (newColor));
currentColor = strtol(newColor, NULL, 16);
Particle.publish("Hex received is ", currentColor);
}*/
int changeToColor(String newColor){
char txt[128];
currentColor = strtol(newColor, NULL, 16);
//snprintf(txt, sizeof(txt), "%s (0x%06x)", (const char*)newColor, currentColor);
//Particle.publish("ColorInfo", txt, PRIVATE);
}
int modeChange(String newMode){
int modeNum = newMode.toInt();
mode = modeNum;
EEPROM.put(eepAddr, modeNum);
}
void runColor(uint32_t currentColor) {
if (currentColor != prevColor) {
//previousMillis = currentMillis;
fill_solid(leds, NUM_LEDS, currentColor);
prevColor = currentColor;
}
}
void newCane(){
int numColors = 2;
int stripeLength = 10;
int travelSpeed = 75;
int color1 = CRGB::White;
int color2 = CRGB::Red;
if (currentMillis - previousMillis >= travelSpeed) {
previousMillis = currentMillis;
for(int i=0;i<NUM_LEDS;i++){
if( (i+offset)%(numColors*stripeLength)<stripeLength+1 ){
leds[i] = color2;
}else{
leds[i] = color1;
}
}
FastLED.show();
offset++;
if(offset>=NUM_LEDS) offset = 0;
/* int L;
for(int j=0;j<(60*5);j++) {
for(int i=0;i< NUM_LEDS;i++) {
L=NUM_LEDS-i-1;
if ( ((i+j) % (5*2) )<5){
leds[L] = CRGB::White;
}else{
leds[L] = CRGB::Red;
}
FastLED.show();
delay(200);
};
}*/
}
}
void runRainbow() {
if (currentMillis - previousMillis >= 30) {
previousMillis = currentMillis;
static uint8_t hue = 0;
fill_rainbow(leds, NUM_LEDS, hue++);
FastLED.show();
}
}
void twinkle() {
//create random twinkle
int rp = random(500,2000);
if (currentMillis - twinkleMillis >= rp) {
twinkleMillis = currentMillis;
int pixel = random(NUM_LEDS);
leds[pixel] = CRGB::White;
FastLED.show();
if(mode==10){
delay(30);
leds[pixel]=prevColor;
FastLED.show();
}
}
}
void fireworks() {
EVERY_N_MILLISECONDS(30){
fadeToBlackBy(leds, NUM_LEDS, 30);
//Serial.println(maxShells);
for (int i = 0; i < maxShells; i++) {
shells[i].Update();
}
FastLED.show();
};
}
void runPalette(NSFastLED::CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette)
{
if (currentMillis - previousMillis >= 30) {
previousMillis = currentMillis;
static uint8_t startindex = 0;
startindex--;
fill_palette( ledarray, numleds, startindex, (256 / NUM_LEDS) + 1, gCurrentPalette, BRIGHTNESS, LINEARBLEND);
FastLED.show();
}
}
DEFINE_GRADIENT_PALETTE( holly_gp) {
0, 0, 255, 0, //green
48, 0, 255, 0, //green
49, 255, 0, 0, //red
64, 255, 0, 0, //red
65, 0, 255, 0, //green
114, 0, 255, 0, //green
115, 255, 0, 0, //red
118, 255, 0, 0, //red
119, 0, 255, 0, //green
168, 0, 255, 0, //green
169, 255, 0, 0, //red
184, 255, 0, 0, //red
185, 0, 255, 0, //green
234, 0, 255, 0, //green
235, 255, 0, 0, //red
255, 255, 0, 0 //red
};
DEFINE_GRADIENT_PALETTE( candycane_gp) {
0 , 128, 128, 128, //white
32 , 128, 128, 128, //white
33 , 255, 0, 0, //red
66 , 255, 0, 0, //red
67 , 128, 128, 128, //white
100 , 128, 128, 128, //white
101 , 255, 0, 0, //red
134 , 255, 0, 0, //red
135 , 128, 128, 128, //white
168 , 128, 128, 128, //white
169 , 255, 0, 0, //red
202 , 255, 0, 0, //red
203 , 128, 128, 128, //white
236 , 128, 128, 128, //white
237 , 255, 0, 0, //red
255 , 255, 0, 0 //red
};
DEFINE_GRADIENT_PALETTE( snowynight_gp) {
0, 163, 182, 199,
41, 188, 192, 200,
117, 117, 157, 240,
204, 117, 224, 240,
255, 163, 182, 199
};
DEFINE_GRADIENT_PALETTE( silvergold_gp) {
0, 237,121, 1,
25, 237,121, 1,
51, 229,149, 1,
76, 222,178, 1,
102, 237,215, 59,
127, 255,255,255,
153, 118,164,188,
178, 46,101,145,
204, 19, 60, 95,
229, 4, 31, 56,
255, 4, 31, 56
};
// Gradient palette "es_autumn_04_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/es/autumn/tn/es_autumn_04.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 20 bytes of program space.
DEFINE_GRADIENT_PALETTE( autumn_gp ) {
0, 71, 135, 0,
101, 88, 1, 0,
165, 210, 22, 1,
234, 255,166, 42,
255, 255,166, 42
};
// Gradient palette "grand_old_flag_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/pj/3/tn/grand-old-flag.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 128 bytes of program space.
DEFINE_GRADIENT_PALETTE( grand_old_flag_gp ) {
0, 1, 2,105,
94, 1, 2,105,
99, 199, 1, 7,
104, 199, 1, 7,
109, 255,255,255,
117, 255,255,255,
122, 199, 1, 7,
124, 199, 1, 7,
130, 255,255,255,
137, 255,255,255,
145, 199, 1, 7,
150, 199, 1, 7,
155, 255,255,255,
160, 255,255,255,
165, 199, 1, 7,
170, 199, 1, 7,
175, 255,255,255,
181, 255,255,255,
188, 199, 1, 7,
191, 199, 1, 7,
198, 255,255,255,
204, 255,255,255,
209, 199, 1, 7,
211, 199, 1, 7,
219, 255,255,255,
224, 255,255,255,
229, 199, 1, 7,
234, 199, 1, 7,
239, 255,255,255,
244, 255,255,255,
249, 199, 1, 7,
255, 199, 1, 7
};
// Gradient palette "christmas_candy_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/ocal/tn/christmas-candy.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 44 bytes of program space.
DEFINE_GRADIENT_PALETTE( christmas_candy_gp ) {
0, 255,255,255,
25, 255, 0, 0,
51, 255,255,255,
76, 0, 55, 0,
102, 255,255,255,
127, 255, 0, 0,
153, 255,255,255,
178, 0, 55, 0,
204, 255,255,255,
229, 255, 0, 0,
255, 255,255,255
};
// Gradient palette "green_purple_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/km/tn/green-purple.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 260 bytes of program space.
DEFINE_GRADIENT_PALETTE( green_purple_gp ) {
0, 255, 155, 0,
40, 255, 155, 0,
49, 0, 0, 0,
96, 0, 0, 0,
107, 0, 254, 0,
179, 0, 254, 0,
183, 203, 0,255,
255, 203, 0,255
};
const TProgmemRGBGradientPalettePtr gGradientPalettes[] = {
holly_gp,
candycane_gp,
snowynight_gp,
silvergold_gp,
autumn_gp,
grand_old_flag_gp,
christmas_candy_gp,
green_purple_gp,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment