Skip to content

Instantly share code, notes, and snippets.

@atuline
Created April 15, 2019 12:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuline/030e9cc163c5067ae04cf63a604c4eb7 to your computer and use it in GitHub Desktop.
Save atuline/030e9cc163c5067ae04cf63a604c4eb7 to your computer and use it in GitHub Desktop.
FastLED fade in capability with gamma correction.
/*
* File: basicfadeingamma
*
* By: Andrew Tuline
*
* Date: April, 2019
*
* Based previous work (namely twinklefox) by Mark Kriegsman, this program shows how you can fade-in twinkles by using the fact that a random number generator
* with the same seed will generate the same numbers every time. Combine that with millis and a sine wave and you have twinkles fading in/out.
*
* The problem is that changes above 50 are much less noticeable to the viewer, so the LED gets bright quickly and then stays bright for way too long before
* dimming again. This version also includes gamma correction to change LED brightness so that it appears to change evenly for the viewer.
*
*/
#include "FastLED.h" // FastLED library.
// Fixed definitions cannot change on the fly.
#define LED_DT 12 // Data pin to connect to the strip.
#define LED_CK 11 // Clock pin for WS2801 or APA102.
#define COLOR_ORDER BGR // It's GRB for WS2812 and BGR for APA102.
#define LED_TYPE APA102 // Using APA102, WS2812, WS2801. Don't forget to modify LEDS.addLeds to suit.
#define NUM_LEDS 42 // Number of LED's.
const uint8_t PROGMEM gamma8[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
// Global variables can be changed on the fly.
uint8_t max_bright = 128; // Overall brightness.
struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
void setup() {
LEDS.addLeds<LED_TYPE,LED_DT,LED_CK,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // Use this for WS2801 or APA102
// LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(0xFFB0F0); // Use this for WS2812
FastLED.setBrightness(max_bright);
set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA.
} // setup()
void loop() {
basicfadeingamma();
FastLED.show();
} // loop()
void basicfadeingamma() {
random16_set_seed(535); // The randomizer needs to be re-set each time through the loop in order for the 'random' numbers to be the same each time through.
for (int i = 0; i<NUM_LEDS; i++) {
uint8_t fader = sin8(millis()/random8(10,20)); // The random number for each 'i' will be the same every time.
fader = pgm_read_byte(&gamma8[fader]); // Gamma correction
leds[i] = CHSV(i*20,255, fader); // Now, let's assign to CHSV values.
}
random16_set_seed(millis()); // Re-randomizing the random number seed for other routines.
} // basicfadeingamma()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment