/*button cycles 39 palettes
Adapted from ColorWavesWithPalettes
// Animated shifting color waves, with several cross-fading color palettes.
// by Mark Kriegsman, August 2015
//
// Color palettes courtesy of cpt-city and its contributors:
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/
//
// Color palettes converted for FastLED using "PaletteKnife" v1:
// http://fastled.io/tools/paletteknife/
//
https://gist.github.com/kriegsman/8281905786e8b2632aeb
*/
#include <FastLED.h>
#define DATA_PIN 6 //was PIXEL_PIN
//#define CLK_PIN 4
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 60 //was PIXEL_COUNT
CRGB leds[NUM_LEDS];
//buttoncyler
#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 2
boolean oldState = HIGH;
int mode = 0; // Currently-active animation mode, 0-9
//---------------------------
// ColorWavesWithPalettes
// Animated shifting color waves, with several cross-fading color palettes.
// by Mark Kriegsman, August 2015
//
// Color palettes courtesy of cpt-city and its contributors:
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/
//
// Color palettes converted for FastLED using "PaletteKnife" v1:
// http://fastled.io/tools/paletteknife/
//
#define BRIGHTNESS 255
void setup() {
delay(500); // delay for recovery
pinMode(BUTTON_PIN, INPUT_PULLUP);
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
//.setCorrection(TypicalLEDStrip) // cpt-city palettes have different color balance
.setDither(BRIGHTNESS < 255);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
Serial.begin(9600);
}
// Forward declarations of an array of cpt-city gradient palettes, and
// a count of how many there are. The actual color palette definitions
// are at the bottom of this file.
extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
extern const uint8_t gGradientPaletteCount;
// Current palette number from the 'playlist' of color palettes
uint8_t gCurrentPaletteNumber = 0;
CRGBPalette16 gCurrentPalette( CRGB::Black);
CRGBPalette16 gTargetPalette( gGradientPalettes[0] );
void loop()
{
//------------
// Get current button state.
boolean newState = digitalRead(BUTTON_PIN);
// Check if state changed from high to low (button press).
if ((newState == LOW) && (oldState == HIGH)) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW) { // Yes, still low
gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
}
}
/* about addmod8
uint8_t addmod8 (uint8_t a, uint8_t b, uint8_t m)
Add two numbers, and calculate the modulo of the sum and a third number, M.
In other words, it returns (A+B) % M.
It is designed as a compact mechanism for incrementing a ‘mode’ switch
and wrapping around back to ‘mode 0’ when the switch goes past the end of the available range.
e.g. if you have seven modes, this switches to the next one
and wraps around if needed: mode = addmod8( mode, 1, 7);
*/
// Set the last-read button state to the old state.
oldState = newState;
// Manage the LED state based on mode
//----
/*
EVERY_N_SECONDS( SECONDS_PER_PALETTE ) { //chg palette
gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
}
*/
EVERY_N_MILLISECONDS(40) {
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
}
colorwaves( leds, NUM_LEDS, gCurrentPalette);
FastLED.show();
FastLED.delay(20);
Serial.print("Palette ");
Serial.print(gCurrentPaletteNumber);
Serial.print(" of ");
Serial.println(gGradientPaletteCount);
//Serial.print(", gCPN");
//Serial.print(gGradientPalettes);
}
// This function draws color waves with an ever-changing,
// widely-varying set of parameters, using a color palette.
void colorwaves( CRGB* ledarray, uint16_t numleds, CRGBPalette16& palette)
{
static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;
uint8_t sat8 = beatsin88( 87, 220, 250);
uint8_t brightdepth = beatsin88( 341, 96, 224);
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;//gHue * 256;
uint16_t hueinc16 = beatsin88(113, 300, 1500);
uint16_t ms = millis();
uint16_t deltams = ms - sLastMillis ;
sLastMillis = ms;
sPseudotime += deltams * msmultiplier;
sHue16 += deltams * beatsin88( 400, 5,9);
uint16_t brightnesstheta16 = sPseudotime;
for( uint16_t i = 0 ; i < numleds; i++) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;
uint16_t h16_128 = hue16 >> 7;
if( h16_128 & 0x100) {
hue8 = 255 - (h16_128 >> 1);
} else {
hue8 = h16_128 >> 1;
}
brightnesstheta16 += brightnessthetainc16;
uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);
uint8_t index = hue8;
//index = triwave8( index);
index = scale8( index, 240);
CRGB newcolor = ColorFromPalette( palette, index, bri8);
uint16_t pixelnumber = i;
pixelnumber = (numleds-1) - pixelnumber;
nblend( ledarray[pixelnumber], newcolor, 128);
}
}
// Alternate rendering function just scrolls the current palette
// across the defined LED strip.
void palettetest( CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette)
{
static uint8_t startindex = 0;
startindex--;
fill_palette( ledarray, numleds, startindex, (256 / NUM_LEDS) + 1, gCurrentPalette, 255, LINEARBLEND);
}
// Gradient Color Palette definitions for 33 different cpt-city color palettes.
// 956 bytes of PROGMEM for all of the palettes together,
// +618 bytes of PROGMEM for gradient palette code (AVR).
// 1,494 bytes total for all 34 color palettes and associated code.
// Gradient palette "ib_jul01_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/ing/xmas/tn/ib_jul01.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 16 bytes of program space.
DEFINE_GRADIENT_PALETTE( ib_jul01_gp ) {
0, 194, 1, 1,
94, 1, 29, 18,
132, 57,131, 28,
255, 113, 1, 1};
DEFINE_GRADIENT_PALETTE( prism_gp ) {
0, 255, 0, 0,
17, 255, 55, 0,
33, 255,255, 0,
51, 0,255, 0,
68, 0, 0,255,
84, 88, 0,255,
102, 255, 0, 0,
119, 255, 55, 0,
135, 255,255, 0,
153, 0,255, 0,
170, 0, 0,255,
186, 88, 0,255,
204, 255, 0, 0,
221, 255, 55, 0,
237, 255,255, 0,
255, 0,255, 0};
DEFINE_GRADIENT_PALETTE( Warm_summer_day_gp ) {
0, 255,225, 39,
51, 255,225, 39,
51, 206,255, 38,
102, 206,255, 38,
102, 137,255, 38,
153, 137,255, 38,
153, 84,255, 38,
204, 84,255, 38,
204, 46,255, 38,
255, 46,255, 38};
DEFINE_GRADIENT_PALETTE( d_e_p_t_h_gp ) {
0, 1, 1, 1,
107, 1, 1, 1,
107, 2, 10, 5,
142, 2, 10, 5,
142, 1, 92, 50,
219, 1, 92, 50,
219, 1,135, 82,
232, 1,135, 82,
232, 1,182,111,
255, 1,182,111};
DEFINE_GRADIENT_PALETTE( Another_Earth_gp ) {
0, 73,156, 1,
51, 73,156, 1,
51, 37,138, 11,
102, 37,138, 11,
102, 14,119, 42,
153, 14,119, 42,
153, 3,103,103,
204, 3,103,103,
204, 1, 87,197,
255, 1, 87,197};
DEFINE_GRADIENT_PALETTE( Rationality_gp ) {
0, 1, 33, 25,
51, 1, 33, 25,
51, 0,104, 92,
102, 0,104, 92,
102, 255,255,255,
153, 255,255,255,
153, 68, 73, 93,
204, 68, 73, 93,
204, 26, 29, 45,
255, 26, 29, 45};
DEFINE_GRADIENT_PALETTE( Optimus_Prime_gp ) {
0, 5, 16, 18,
51, 5, 16, 18,
51, 8, 38, 71,
102, 8, 38, 71,
102, 194,189,151,
153, 194,189,151,
153, 167, 6, 2,
204, 167, 6, 2,
204, 53, 1, 1,
255, 53, 1, 1};
DEFINE_GRADIENT_PALETTE( es_seadreams_11_gp ) {
0, 1, 1,240,
255, 1,239,230};
DEFINE_GRADIENT_PALETTE( Gummy_Kids_gp ) {
0, 8, 47, 5,
31, 77,122, 6,
63, 249,237, 7,
95, 232, 51, 1,
127, 215, 0, 1,
159, 47, 1, 3,
191, 1, 7, 16,
223, 52, 22, 6,
255, 239, 45, 1};
DEFINE_GRADIENT_PALETTE( velvet_ocean_gp ) {
0, 1, 79, 80,
51, 1, 79, 80,
51, 1, 62, 62,
102, 1, 62, 62,
102, 1, 43, 40,
153, 1, 43, 40,
153, 1, 27, 23,
204, 1, 27, 23,
204, 1, 13, 10,
255, 1, 13, 10};
DEFINE_GRADIENT_PALETTE( entrance_drive_gp ) {
0, 215,223,223,
63, 215,223,223,
63, 65,217,119,
127, 65,217,119,
127, 1,146,108,
191, 1,146,108,
191, 1, 31, 2,
255, 1, 31, 2};
DEFINE_GRADIENT_PALETTE( Three_girls_reclining_gp ) {
0, 6, 45, 16,
51, 6, 45, 16,
51, 135,201,108,
102, 135,201,108,
102, 227,221,151,
153, 227,221,151,
153, 83,182,186,
204, 83,182,186,
204, 159,127,178,
255, 159,127,178};
DEFINE_GRADIENT_PALETTE( rainbowsherbet_gp ) {
0, 255, 33, 4,
43, 255, 68, 25,
86, 255, 7, 25,
127, 255, 82,103,
170, 255,255,242,
209, 42,255, 22,
255, 87,255, 65};
DEFINE_GRADIENT_PALETTE( tiger_gp ) {
0, 255, 97, 0,
12, 255, 97, 0,
19, 0, 0, 0,
31, 0, 0, 0,
63, 255, 97, 0,
89, 255, 97, 0,
114, 0, 0, 0,
140, 0, 0, 0,
165, 255, 97, 0,
191, 255, 97, 0,
223, 0, 0, 0,
235, 0, 0, 0,
242, 255, 97, 0,
255, 255, 97, 0};
DEFINE_GRADIENT_PALETTE( yellow_red_yellow_gp ) {
0, 255,255, 0,
127, 255, 0, 0,
255, 255,255, 0};
DEFINE_GRADIENT_PALETTE( radial_eyeball_light_blue_gp ) {
0, 0, 0, 0,
25, 0, 0, 0,
38, 7,138,255,
51, 7,138,255,
102, 0, 0,255,
109, 0, 0,255,
114, 7,138,255,
121, 255,255,255,
229, 255,255,255,
249, 255,191,184,
255, 255, 0, 0};
DEFINE_GRADIENT_PALETTE( metal_steel_blue_gp ) {
0, 1, 27,103,
63, 197,209,228,
127, 1, 27,103,
191, 197,209,228,
255, 1, 27,103};
// Single array of defined cpt-city color palettes.
// This will let us programmatically choose one based on a number, rather than having to activate each explicitly
// by name every time. Since it is const, this array could also be moved
// into PROGMEM to save SRAM, but for simplicity of illustration we'll keep it in a regular SRAM array.
// This list of color palettes acts as a "playlist"; you can add or delete, or re-arrange as you wish.
const TProgmemRGBGradientPalettePtr gGradientPalettes[] = {
prism_gp,
Warm_summer_day_gp,
d_e_p_t_h_gp,
Another_Earth_gp,
Rationality_gp,
Optimus_Prime_gp,
es_seadreams_11_gp,
Gummy_Kids_gp,
velvet_ocean_gp,
entrance_drive_gp,
Three_girls_reclining_gp,
rainbowsherbet_gp,
tiger_gp,
yellow_red_yellow_gp,
radial_eyeball_light_blue_gp,
metal_steel_blue_gp
};
// Count of how many cpt-city gradients are defined:
const uint8_t gGradientPaletteCount =
sizeof( gGradientPalettes) / sizeof( TProgmemRGBGradientPalettePtr );