Last active
April 7, 2022 08:25
-
-
Save perigalacticon/9b49b3b18b358a28fc597d38d0366f5a to your computer and use it in GitHub Desktop.
modify inoise8_pal_demo_fastLed.ino to use custom palettes arbitrarily.
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
// https://pastebin.com/r70Qk6Bn | |
/* Title: inoise8_pal_demo.ino | |
* | |
* By: Andrew Tuline | |
* | |
* Added custom color palettes arbitrarily selectable. | |
*/ | |
#include "FastLED.h" | |
#if FASTLED_VERSION < 3001000 | |
#error "Requires FastLED 3.1 or later; check github for latest code." | |
#endif | |
#define LED_PIN 2 | |
//#define CLK_PIN 11 | |
#define BRIGHTNESS 255 | |
#define LED_TYPE WS2812B // Only use the LED_PIN for WS2812's | |
#define COLOR_ORDER GRB | |
#define NUM_LEDS 43 | |
struct CRGB leds[NUM_LEDS]; | |
static uint16_t dist; // A random number for our noise generator. | |
uint16_t scale = 30; // Wouldn't recommend changing this on the fly, or the animation will be really blocky. | |
uint8_t maxChanges = 48; // Value for blending between palettes. | |
// Summer fades to Autumn Color Palette: | |
// SFTA | |
// http://www.color-hex.com/color-palette/10894 | |
CRGB SFTA0 = CRGB(88,140,115); | |
CRGB SFTA1 = CRGB(242,174,148); | |
CRGB SFTA2 = CRGB(242,174,114); | |
CRGB SFTA3 = CRGB(217,100,89); | |
CRGB SFTA4 = CRGB(140,70,70); | |
//extern CRGBPalette16 summerFadesToAutumnPalette; | |
//extern const TProgmemPalette16 summerFadesToAutumnPalette_p PROGMEM; | |
const TProgmemPalette16 summerFadesToAutumnPalette_p PROGMEM = | |
{ | |
SFTA0, | |
SFTA1, | |
SFTA2, | |
SFTA3, | |
SFTA4, | |
SFTA0, | |
SFTA1, | |
SFTA2, | |
SFTA3, | |
SFTA4, | |
SFTA0, | |
SFTA1, | |
SFTA2, | |
SFTA3, | |
SFTA4, | |
SFTA0 | |
}; | |
// Autumn Color Palette: | |
// http://www.color-hex.com/color-palette/1278 | |
CRGB AC0 = CRGB(255,210,0); | |
CRGB AC1 = CRGB(155,87,8); | |
CRGB AC2 = CRGB(244,123,32); | |
CRGB AC3 = CRGB(247,151,98); | |
CRGB AC4 = CRGB(240,81,51); | |
//extern CRGBPalette16 autumnColorPalette; | |
//extern const TProgmemPalette16 autumnColorPalette_p PROGMEM; | |
const TProgmemPalette16 autumnColorPalette_p PROGMEM = | |
{ | |
AC0, | |
AC1, | |
AC2, | |
AC3, | |
AC4, | |
AC0, | |
AC1, | |
AC2, | |
AC3, | |
AC4, | |
AC0, | |
AC1, | |
AC2, | |
AC3, | |
AC4, | |
AC0 | |
}; | |
// Colors of Autumn Color Palette | |
// http://www.color-hex.com/color-palette/10153 | |
CRGB COA0 = CRGB(251,54,54); | |
CRGB COA1 = CRGB(234,199,56); | |
CRGB COA2 = CRGB(240,249,97); | |
CRGB COA3 = CRGB(210,236,0); | |
CRGB COA4 = CRGB(122,195,19); | |
//extern CRGBPalette16 colorsOfAutumnPalette; | |
//extern const TProgmemPalette16 colorsOfAutumnPalette PROGMEM; | |
const TProgmemPalette16 colorsOfAutumnPalette_p PROGMEM = | |
{ | |
COA0, | |
COA1, | |
COA2, | |
COA3, | |
COA4, | |
COA0, | |
COA1, | |
COA2, | |
COA3, | |
COA4, | |
COA0, | |
COA1, | |
COA2, | |
COA3, | |
COA4, | |
COA0 | |
}; | |
CRGBPalette16 currentPalette(CRGB::Black); | |
CRGBPalette16 targetPalette(summerFadesToAutumnPalette_p); | |
const TProgmemRGBPalette16* ActivePaletteList[] = { | |
summerFadesToAutumnPalette_p, | |
autumnColorPalette_p, | |
colorsOfAutumnPalette_p | |
}; | |
void setup() | |
{ | |
Serial.begin(57600); | |
delay(3000); | |
LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS); | |
LEDS.setBrightness(BRIGHTNESS); | |
dist = random16(12345); // A semi-random number for our noise generator | |
chooseNextColorPalette(targetPalette); | |
} | |
void loop() { | |
EVERY_N_MILLISECONDS(10) { | |
nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // Blend towards the target palette | |
fillnoise8(); // Update the LED array with noise at the new location | |
} | |
EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds. | |
chooseNextColorPalette( targetPalette ); | |
} | |
LEDS.show(); // Display the LED's at every loop cycle. | |
} | |
void fillnoise8() { | |
for(int i = 0; i < NUM_LEDS; i++) { // Just ONE loop to fill up the LED array as all of the pixels change. | |
uint8_t index = inoise8(i*scale, dist+i*scale) % 255; // Get a value from the noise function. I'm using both x and y axis. | |
leds[i] = ColorFromPalette(currentPalette, index, 255, LINEARBLEND); // With that value, look up the 8 bit colour palette value and assign it to the current LED. | |
} | |
dist += beatsin8(10,1, 4); // Moving along the distance (that random number we started out with). Vary it a bit with a sine wave. | |
// In some sketches, I've used millis() instead of an incremented counter. Works a treat. | |
} | |
// Advance to the next color palette in the list (above). | |
void chooseNextColorPalette( CRGBPalette16& pal) | |
{ | |
const uint8_t numberOfPalettes = sizeof(ActivePaletteList) / sizeof(ActivePaletteList[0]); | |
static uint8_t whichPalette = -1; | |
whichPalette = addmod8( whichPalette, 1, numberOfPalettes); | |
pal = *(ActivePaletteList[whichPalette]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment