Instantly share code, notes, and snippets.
Created
March 23, 2015 10:59
Star
You must be signed in to star a gist
Control FastLED palettes with buttons
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
// this sketch makes use of a button library. | |
// get the buttons lib from here: | |
// http://playground.arduino.cc/Code/Buttons | |
#include <buttons.h> | |
#include <FastLED.h> | |
// define the LEDs | |
#define LED_PIN 12 //pin the LEDs are connected to | |
#define NUM_LEDS 50 | |
#define MAX_BRIGHTNESS 110 //maximum brightness - useful if running off battery | |
#define LED_TYPE WS2811 | |
#define COLOR_ORDER RGB | |
struct CRGB leds[NUM_LEDS]; | |
#define UPDATES_PER_SECOND 100 | |
CRGBPalette16 currentPalette; | |
TBlendType currentBlending; | |
extern CRGBPalette16 myRedWhiteBluePalette; | |
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM; | |
// define the buttons that we'll use. | |
// one to control brightness and one to select palette. | |
Button paletteButton, brightnessButton; | |
// some counters to keep state | |
int paletteCounter = 1; | |
int brightnessCounter = 32; | |
void setup() { | |
delay( 3000 ); // power-up safety delay - DO NOT REMOVE | |
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); | |
FastLED.setBrightness(32); // start off 1/8 brightness | |
FastLED.clear(); | |
// set up buttons | |
paletteButton.setMode(OneShot); | |
brightnessButton.setMode(OneShot); | |
// pin 8 = brightness button | |
// pin 4 = palette button | |
paletteButton.assign(4); | |
brightnessButton.assign(8); | |
} | |
void loop() { | |
switch (brightnessButton.check()) { // which button is pressed | |
case ON: | |
// 8 brightness | |
brightnessCounter+=8; // brightness = 0-255, so steps of 8 | |
if (brightnessCounter >= MAX_BRIGHTNESS) { | |
brightnessCounter = 32; // we roll over to 1/8 bright | |
} | |
break; | |
default: | |
break; | |
} | |
switch(paletteButton.check()){ | |
case ON: | |
paletteCounter++; | |
if (paletteCounter > 33) { // adjust if you have more or less than 34 palettes | |
paletteCounter = 0; | |
} | |
break; | |
default: | |
break; | |
} | |
ChangePalettePeriodically(); | |
static uint8_t startIndex = 0; | |
startIndex = startIndex + 1; | |
FillLEDsFromPaletteColors(startIndex); | |
FastLED.show(); | |
FastLED.delay(1000 / UPDATES_PER_SECOND); | |
} | |
extern CRGBPalette16 myRedWhiteBluePalette; | |
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM; | |
void fillSolid(uint32_t colour) { | |
fill_solid(leds, NUM_LEDS, colour); | |
} | |
void FillLEDsFromPaletteColors( uint8_t colorIndex) | |
{ | |
uint8_t brightness = 255; | |
for( int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); | |
colorIndex += 3; | |
} | |
} | |
// There are several different palettes of colors demonstrated here. | |
// | |
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p, | |
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p. | |
// | |
// Additionally, you can manually define your own color palettes, or you can write | |
// code that creates color palettes on the fly. All are shown here. | |
void ChangePalettePeriodically() | |
{ | |
if( paletteCounter == 0) { fill_solid( currentPalette, 16, CRGB::Red); currentBlending = NOBLEND;} | |
if( paletteCounter == 1) { fill_solid( currentPalette, 16, CRGB::Crimson); currentBlending = NOBLEND;} | |
if( paletteCounter == 2) { fill_solid( currentPalette, 16, CRGB::Orange); currentBlending = NOBLEND;} | |
if( paletteCounter == 3) { fill_solid( currentPalette, 16, CRGB::DarkOrange); currentBlending = NOBLEND;} | |
if( paletteCounter == 4) { fill_solid( currentPalette, 16, CRGB::Yellow); currentBlending = NOBLEND;} | |
if( paletteCounter == 5) { fill_solid( currentPalette, 16, CRGB::Gold); currentBlending = NOBLEND;} | |
if( paletteCounter == 6) { fill_solid( currentPalette, 16, CRGB::Green); currentBlending = NOBLEND;} | |
if( paletteCounter == 7) { fill_solid( currentPalette, 16, CRGB::GreenYellow); currentBlending = NOBLEND;} | |
if( paletteCounter == 8) { fill_solid( currentPalette, 16, CRGB::Lime); currentBlending = NOBLEND;} | |
if( paletteCounter == 9) { fill_solid( currentPalette, 16, CRGB::Blue); currentBlending = NOBLEND;} | |
if( paletteCounter == 10) { fill_solid( currentPalette, 16, CRGB::Aqua); currentBlending = NOBLEND;} | |
if( paletteCounter == 11) { fill_solid( currentPalette, 16, CRGB::DarkTurquoise); currentBlending = NOBLEND;} | |
if( paletteCounter == 12) { fill_solid( currentPalette, 16, CRGB::Navy); currentBlending = NOBLEND;} | |
if( paletteCounter == 13) { fill_solid( currentPalette, 16, CRGB::Purple); currentBlending = NOBLEND;} | |
if( paletteCounter == 14) { fill_solid( currentPalette, 16, CRGB::Violet); currentBlending = NOBLEND;} | |
if( paletteCounter == 15) { fill_solid( currentPalette, 16, CRGB::DeepPink); currentBlending = NOBLEND;} | |
if( paletteCounter == 16) { fill_solid( currentPalette, 16, CRGB::LightCoral); currentBlending = NOBLEND;} | |
if( paletteCounter == 17) { fill_solid( currentPalette, 16, CRGB::White); currentBlending = NOBLEND;} | |
if( paletteCounter == 18) { fill_solid( currentPalette, 16, CRGB::BurlyWood); currentBlending = NOBLEND;} | |
if( paletteCounter == 19) { fill_solid( currentPalette, 16, CRGB::Thistle); currentBlending = NOBLEND;} | |
if( paletteCounter == 20) { fill_solid( currentPalette, 16, CRGB::White); currentBlending = NOBLEND;} | |
if( paletteCounter == 21) { currentPalette = RainbowColors_p; currentBlending = NOBLEND; } | |
if( paletteCounter == 22) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; } | |
if( paletteCounter == 23) { currentPalette = OceanColors_p; currentBlending = NOBLEND; } | |
if( paletteCounter == 24) { currentPalette = CloudColors_p; currentBlending = NOBLEND; } | |
if( paletteCounter == 25) { currentPalette = LavaColors_p; currentBlending = NOBLEND; } | |
if( paletteCounter == 26) { currentPalette = ForestColors_p; currentBlending = NOBLEND; } | |
if( paletteCounter == 27) { currentPalette = PartyColors_p; currentBlending = NOBLEND; } | |
if( paletteCounter == 28) { SetupPurpleAndGreenPalette(); currentBlending = BLEND; } | |
if( paletteCounter == 29) { SetupTotallyRandomPalette(); currentBlending = BLEND; } | |
if( paletteCounter == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; } | |
if( paletteCounter == 31) { SetupBlackAndWhiteStripedPalette(); currentBlending = BLEND; } | |
if( paletteCounter == 32) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; } | |
if( paletteCounter == 33) { currentPalette = myRedWhiteBluePalette_p; currentBlending = BLEND; } | |
} | |
// This function fills the palette with totally random colors. | |
void SetupTotallyRandomPalette() | |
{ | |
for( int i = 0; i < 16; i++) { | |
currentPalette[i] = CHSV( random8(), 255, random8()); | |
} | |
} | |
// This function sets up a palette of black and white stripes, | |
// using code. Since the palette is effectively an array of | |
// sixteen CRGB colors, the various fill_* functions can be used | |
// to set them up. | |
void SetupBlackAndWhiteStripedPalette() | |
{ | |
// 'black out' all 16 palette entries... | |
fill_solid( currentPalette, 16, CRGB::Black); | |
// and set every fourth one to white. | |
currentPalette[0] = CRGB::White; | |
currentPalette[4] = CRGB::White; | |
currentPalette[8] = CRGB::White; | |
currentPalette[12] = CRGB::White; | |
} | |
// This function sets up a palette of purple and green stripes. | |
void SetupPurpleAndGreenPalette() | |
{ | |
CRGB purple = CHSV( HUE_PURPLE, 255, 255); | |
CRGB green = CHSV( HUE_GREEN, 255, 255); | |
CRGB black = CRGB::Black; | |
currentPalette = CRGBPalette16( | |
green, green, black, black, | |
purple, purple, black, black, | |
green, green, black, black, | |
purple, purple, black, black ); | |
} | |
// This example shows how to set up a static color palette | |
// which is stored in PROGMEM (flash), which is almost always more | |
// plentiful than RAM. A static PROGMEM palette like this | |
// takes up 64 bytes of flash. | |
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM = | |
{ | |
CRGB::Red, | |
CRGB::Gray, // 'white' is too bright compared to red and blue | |
CRGB::Blue, | |
CRGB::Black, | |
CRGB::Red, | |
CRGB::Gray, | |
CRGB::Blue, | |
CRGB::Black, | |
CRGB::Red, | |
CRGB::Red, | |
CRGB::Gray, | |
CRGB::Gray, | |
CRGB::Blue, | |
CRGB::Blue, | |
CRGB::Black, | |
CRGB::Black | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment