Skip to content

Instantly share code, notes, and snippets.

@brantritter
Created January 26, 2018 00:52
Show Gist options
  • Save brantritter/c63fc7560349e2456a11934c7b6a8d83 to your computer and use it in GitHub Desktop.
Save brantritter/c63fc7560349e2456a11934c7b6a8d83 to your computer and use it in GitHub Desktop.
LED color fade
//***************************************************************
// Example of blending from one color to a target color.
// Once the target color is reached a new random target color
// is picked to blend toward.
//
// leds[0] always displays the target color.
//
// Marc Miller, Feb 2017
//***************************************************************
#include "FastLED.h"
#define DATA_PIN 11
#define LED_TYPE WS2811_400
#define COLOR_ORDER GRB
#define NUM_LEDS 240
#define BRIGHTNESS 100
CRGB leds[NUM_LEDS];
uint8_t blendRate = 5; // How fast to blend. Higher is slower. [milliseconds]
CHSV colorStart = CHSV(); // starting color
CHSV colorStart2 = CHSV(); // starting color
CHSV colorTarget = CHSV(); // target color
CHSV colorCurrent = colorStart; //defines colorCurrent
//---------------------------------------------------------------
void setup() {
Serial.begin(115200); // Allows serial monitor output (check baud rate)
delay(3000); // 3 second delay for recovery
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
//---------------------------------------------------------------
void loop()
{
EVERY_N_MILLISECONDS(blendRate) {
static uint8_t k;
colorStart = CHSV(5, 255, 255); //red
if ( colorCurrent == colorTarget ) { // Check if target has been reached
colorTarget = CHSV(85, 255, 255); //green
colorStart = colorCurrent;
k = 0; // reset k value
Serial.print("New colorTarget:\t\t\t"); Serial.println(colorTarget.h);
}
colorStart =CHSV(5, 254, 255); //green
if ( colorCurrent == colorTarget ) { // Check if target has been reached
colorTarget = CHSV(85, 255, 255); //red
colorStart = colorCurrent;
k = 0; // reset k value
}
colorCurrent = blend(colorStart, colorTarget, k, SHORTEST_HUES);
fill_solid( leds, NUM_LEDS, colorCurrent );
leds[0] = colorTarget; // set first pixel to always show target color
Serial.print("colorCurrent:\t"); Serial.print(colorCurrent.h); Serial.print("\t");
Serial.print("colorTarget:\t"); Serial.print(colorTarget.h);
Serial.print("\tk: "); Serial.println(k);
k++;
}
FastLED.show(); // update the display
}
//---------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment