Instantly share code, notes, and snippets.
Created
September 24, 2017 23:23
-
Save slurrybowl/805f5d396ec0c524dfb098a3fe35b5db to your computer and use it in GitHub Desktop.
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
#include "FastLED.h" | |
FASTLED_USING_NAMESPACE | |
#if FASTLED_VERSION < 3001000 | |
#error "Requires FastLED 3.1 or later; check github for latest code." | |
#endif | |
#define DATA_PIN 4 | |
//#define CLK_PIN 4 | |
#define LED_TYPE WS2811 | |
#define COLOR_ORDER GRB | |
#define NUM_LEDS 240 | |
CRGB leds[NUM_LEDS]; | |
CRGBPalette16 gPal; | |
gPal = CRGBPalette16( CRGB::Green, CRGB::Black, CRGB::Blue, CRGB::Aqua); | |
#define BRIGHTNESS 95 | |
#define FRAMES_PER_SECOND 120 | |
#include "Button.h" // Include Button library | |
const int buttonPin = 6; // Set digital pin used with debounced pushbutton | |
Button myButton(buttonPin, true, true, 50); // Declare the butto | |
void setup() { | |
delay(3000); // 3 second delay for recovery | |
// tell FastLED about the LED strip configuration | |
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | |
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | |
// set master brightness control | |
FastLED.setBrightness(BRIGHTNESS); | |
} | |
// List of patterns to cycle through. Each is defined as a separate function below. | |
typedef void (*SimplePatternList[])(); | |
//SimplePatternList gPatterns = { rainbow,juggle_Bchill, rainbowuniform, rainbowWEIRD, rainbowWithGlitter, confetti_GB, confetti, juggle, juggle_B, juggle_R, juggle_G, sinelon }; | |
SimplePatternList gPatterns = { juggle_B_less,juggle_B_more,juggle_B_lots,juggle_Bsteady, confetti_GB, juggle_G, juggle_R,gPatterns }; | |
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current | |
uint8_t gHue = 0; // rotating "base color" used by many of the patterns | |
void loop() | |
{ | |
// Call the current pattern function once, updating the 'leds' array | |
gPatterns[gCurrentPatternNumber](); | |
// send the 'leds' array out to the actual LED strip | |
FastLED.show(); | |
// insert a delay to keep the framerate modest | |
FastLED.delay(1000/FRAMES_PER_SECOND); | |
// do some periodic updates | |
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow | |
//EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically | |
readbutton(); // check for button press | |
} | |
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) | |
void nextPattern() | |
{ | |
// add one to the current pattern number, and wrap around at the end | |
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns); | |
} | |
void rainbow() | |
{ | |
// FastLED's built-in rainbow generator | |
fill_rainbow( leds, NUM_LEDS, gHue, 7); | |
} | |
void rainbowuniform() | |
{ | |
// FastLED's built-in rainbow generator | |
fill_rainbow( leds, NUM_LEDS, gHue, 2); | |
} | |
void rainbowWEIRD() | |
{ | |
// FastLED's built-in rainbow generator | |
fill_rainbow( leds, NUM_LEDS, gHue, 30); | |
} | |
void sinelon() | |
{ | |
// a colored dot sweeping back and forth, with fading trails | |
fadeToBlackBy( leds, NUM_LEDS, 20); | |
int pos = beatsin16(13,0,NUM_LEDS); | |
leds[pos] += CHSV( gHue, 255, 192); | |
} | |
void confetti() | |
{ | |
// random colored speckles that blink in and fade smoothly | |
fadeToBlackBy( leds, NUM_LEDS, 10); | |
int pos = random16(NUM_LEDS); | |
leds[pos] += CHSV( gHue + random8(64), 20, 250); | |
} | |
void rainbowWithGlitter() | |
{ | |
// built-in FastLED rainbow, plus some random sparkly glitter | |
rainbow(); | |
addGlitter(80); | |
} | |
void addGlitter( fract8 chanceOfGlitter) | |
{ | |
if( random8() < chanceOfGlitter) { | |
leds[ random16(NUM_LEDS) ] += CRGB::White; | |
} | |
} | |
void juggle_R() { | |
// eight colored dots, weaving in and out of sync with each other | |
fadeToBlackBy( leds, NUM_LEDS, 20); | |
byte dothue = 5; | |
for( int i = 0; i < 8; i++) { | |
leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100); | |
//dothue += 32; | |
} | |
} | |
void juggle_G() { | |
// eight colored dots, weaving in and out of sync with each other | |
fadeToBlackBy( leds, NUM_LEDS, 200); | |
byte dothue = 96; | |
for( int i = 0; i < 8; i++) { | |
leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100); | |
//dothue += 32; | |
} | |
} | |
void juggle_B_less() { | |
// eight colored dots, weaving in and out of sync with each other | |
fadeToBlackBy( leds, NUM_LEDS, 240); | |
byte dothue = 160; | |
for( int i = 0; i < 8; i++) { | |
leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100); | |
//dothue += 32; | |
} | |
} | |
void juggle_B_more() { | |
// eight colored dots, weaving in and out of sync with each other | |
fadeToBlackBy( leds, NUM_LEDS, 100); | |
byte dothue = 160; | |
for( int i = 0; i < 8; i++) { | |
leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100); | |
//dothue += 32; | |
} | |
} | |
void juggle_B_lots() { | |
// eight colored dots, weaving in and out of sync with each other | |
fadeToBlackBy( leds, NUM_LEDS, 10); | |
byte dothue = 160; | |
for( int i = 0; i < 8; i++) { | |
leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100); | |
//dothue += 32; | |
} | |
} | |
void juggle_Bsteady() { | |
// eight colored dots, weaving in and out of sync with each other | |
fadeToBlackBy( leds, NUM_LEDS, 1); | |
byte dothue = 160; | |
for( int i = 0; i < 240; i++) { | |
leds[beatsin16(i+200,0,NUM_LEDS)] |= CHSV(dothue, 200, 55); | |
//dothue += 32; | |
} | |
} | |
void juggle() { | |
// eight colored dots, weaving in and out of sync with each other | |
fadeToBlackBy( leds, NUM_LEDS, 20); | |
byte dothue = 0; | |
for( int i = 0; i < 8; i++) { | |
leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 100); | |
dothue += 32; | |
} | |
} | |
void confetti_GB() | |
{ | |
// random colored speckles, Green and Blue hues only | |
// Green = 96, Blue = 160 | |
uint8_t p = 30; // What percentage of the time to make speckles. [Range 0-100] | |
fadeToBlackBy( leds, NUM_LEDS, 10); | |
if (random8(100) < p) { | |
int pos = random16(NUM_LEDS); | |
uint8_t hue = random8(2); // randomly chooses a 0 or 1 | |
if (hue == 0) { | |
hue = random8(92,111); // pick a hue somewhere in the green zone | |
} else { | |
hue = random8(156,165); // pick a hue somewhere in the blue zone | |
} | |
leds[pos] += CHSV( hue, random8(200,240), 100); | |
} | |
}//end confetti_GB | |
Fire2012WithPalette(); // run simulation frame, using palette colors | |
FastLED.show(); // display this frame | |
FastLED.delay(1000 / FRAMES_PER_SECOND); | |
} | |
// Fire2012 by Mark Kriegsman, July 2012 | |
// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY | |
//// | |
// This basic one-dimensional 'fire' simulation works roughly as follows: | |
// There's a underlying array of 'heat' cells, that model the temperature | |
// at each point along the line. Every cycle through the simulation, | |
// four steps are performed: | |
// 1) All cells cool down a little bit, losing heat to the air | |
// 2) The heat from each cell drifts 'up' and diffuses a little | |
// 3) Sometimes randomly new 'sparks' of heat are added at the bottom | |
// 4) The heat from each cell is rendered as a color into the leds array | |
// The heat-to-color mapping uses a black-body radiation approximation. | |
// | |
// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot). | |
// | |
// This simulation scales it self a bit depending on NUM_LEDS; it should look | |
// "OK" on anywhere from 20 to 100 LEDs without too much tweaking. | |
// | |
// I recommend running this simulation at anywhere from 30-100 frames per second, | |
// meaning an interframe delay of about 10-35 milliseconds. | |
// | |
// Looks best on a high-density LED setup (60+ pixels/meter). | |
// | |
// | |
// There are two main parameters you can play with to control the look and | |
// feel of your fire: COOLING (used in step 1 above), and SPARKING (used | |
// in step 3 above). | |
// | |
// COOLING: How much does the air cool as it rises? | |
// Less cooling = taller flames. More cooling = shorter flames. | |
// Default 55, suggested range 20-100 | |
#define COOLING 100 | |
// SPARKING: What chance (out of 255) is there that a new spark will be lit? | |
// Higher chance = more roaring fire. Lower chance = more flickery fire. | |
// Default 120, suggested range 50-200. | |
#define SPARKING 120 | |
void Fire2012WithPalette() | |
{ | |
// Array of temperature readings at each simulation cell | |
static byte heat[NUM_LEDS]; | |
// Step 1. Cool down every cell a little | |
for( int i = 0; i < NUM_LEDS; i++) { | |
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2)); | |
} | |
// Step 2. Heat from each cell drifts 'up' and diffuses a little | |
for( int k= NUM_LEDS - 1; k >= 2; k--) { | |
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; | |
} | |
// Step 3. Randomly ignite new 'sparks' of heat near the bottom | |
if( random8() < SPARKING ) { | |
int y = random8(7); | |
heat[y] = qadd8( heat[y], random8(160,255) ); | |
} | |
// Step 4. Map from heat cells to LED colors | |
for( int j = 0; j < NUM_LEDS; j++) { | |
// Scale the heat value from 0-255 down to 0-240 | |
// for best results with color palettes. | |
byte colorindex = scale8( heat[j], 240); | |
CRGB color = ColorFromPalette( gPal, colorindex); | |
int pixelnumber; | |
if( gReverseDirection ) { | |
pixelnumber = (NUM_LEDS-1) - j; | |
} else { | |
pixelnumber = j; | |
} | |
leds[pixelnumber] = color; | |
} | |
} | |
//BUTTON STUFF | |
//---------Function to read the button and do something---------- | |
void readbutton() { | |
myButton.read(); | |
if(myButton.wasPressed()) { | |
nextPattern(); // Change to the next pattern | |
} | |
}//end_readbutton | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment