#include "FastLED.h"
const int throttle1 = 75;
const int throttle2 = 200;
#define FASTLED_ALLOW_INTERRUPTS 0 // Used for ESP8266.
#define THROTTLE_CHANNEL A0
#define LED_PIN 2
#define CLK_PIN D4
#define BRIGHTNESS 255
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 255
uint8_t heatindex = 0;
uint8_t throttle = 0;
uint16_t skipPallette = 0;
struct CRGB leds[NUM_LEDS];
// Crossfade current palette slowly toward the target palette
//
// Each time that nblendPaletteTowardPalette is called, small changes
// are made to currentPalette to bring it closer to matching targetPalette.
// You can control how many changes are made in each call:
// - the default of 24 is a good balance
// - meaningful values are 1-48. 1=veeeeeeeery slow, 48=quickest
// - "0" means do not change the currentPalette at all; freeze
uint8_t maxBlendChanges = 24;
TBlendType currentBlending;
DEFINE_GRADIENT_PALETTE( my_coolp ) {
0, 0, 0, 0, //black
128, 9, 9, 121, //blue
224, 186,99, 187, //purple
250, 255,216, 0, // yellow
255, 0,0,255 }; //full white
DEFINE_GRADIENT_PALETTE( my_hotP ) {
0, 252,236, 69, //black
64, 253, 29, 29, //red
128, 131,58, 180, //ultra Violet
190, 252,236, 69, //bright yellow
255, 252,236, 69 }; //full white
CRGBPalette16 oldTargetPalette;
CRGBPalette16 currentPalette;
CRGBPalette16 upperPalette;
CRGBPalette16 lowerPallette;
CRGBPalette16 myBasePalette( CRGB::Black);
CRGBPalette16 myCoolPal = my_coolp;
CRGBPalette16 myHotPal = my_hotP;
// If the channel is off, return the default value
int readChannel(int channelInput, int minLimit, int maxLimit, int defaultValue){
int ch = analogRead(channelInput);
int upperSignalLimit = 1023; //1023
return map(ch, 0, upperSignalLimit, minLimit, maxLimit);
}
void visualizeSlider(int paintToIndex) {
if (paintToIndex > 254) paintToIndex = 254;
for(int i = 0; i < paintToIndex; i++){
leds[i] = CRGB (255, 255, 255);
}
}
void setup() {
currentBlending = LINEARBLEND;
Serial.begin(9600);//full speed but can be any other speed
// put your setup code here, to run once:
LEDS.clear();
LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
LEDS.setBrightness(BRIGHTNESS);
pinMode(THROTTLE_CHANNEL, INPUT);
skipPallette = ceil(255/NUM_LEDS);
fill_palette(leds, NUM_LEDS, 0, skipPallette, myBasePalette, 250, LINEARBLEND);
} // setup()
void loop() {
throttle = readChannel(THROTTLE_CHANNEL, 0, 254, 0);
//for(int i = 0 ; i < NUM_LEDS; i++) {
// leds[i] = ColorFromPalette(myPal, heatindex);
// }
//nblend( leds, hotleds, 20, 100);
fract8 blendAmount = 0;
int rangeStart = 0;
int rangeEnd = 0;
if (throttle < throttle1 ) {
lowerPallette = myBasePalette;
upperPalette = myCoolPal;
oldTargetPalette = myBasePalette;
rangeStart = 0;
rangeEnd = throttle1;
}
else if (throttle < throttle2) {
upperPalette = myHotPal;
lowerPallette = myCoolPal;
oldTargetPalette = myCoolPal;
rangeStart = throttle1;
rangeEnd = throttle2;
}
else if (throttle > throttle2) {
lowerPallette = myHotPal;
upperPalette = myHotPal;
oldTargetPalette = myHotPal;
rangeStart = throttle2;
rangeEnd = 255;
}
blendAmount = map(throttle, rangeStart, rangeEnd, 0, 255);
//nblendPaletteTowardPalette(currentPalette, oldTargetPalette, maxBlendChanges);
for( int i = 0; i < NUM_LEDS; i++) {
CRGB lowerCol = ColorFromPalette( lowerPallette, i);
CRGB upperCol = ColorFromPalette( upperPalette, i);
CRGB targetColor = blend(lowerCol, upperCol, blendAmount);
leds[i] = blend(leds[i], targetColor, 5);
}
throttle = 255;
//fill_palette(leds, NUM_LEDS, 0, skipPallette, upperPalette, throttle, LINEARBLEND);
//fill_palette(leds, NUM_LEDS, 0, skipPallette, currentPalette, throttle, LINEARBLEND);
//visualizeSlider(rangePos);
LEDS.show(); // Display the LED's at every loop cycle.
} // loop()
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i*16), brightness);
colorIndex += 3;
}
}