Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kriegsman
Created August 25, 2015 21:09
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kriegsman/88954aae22b03a664081 to your computer and use it in GitHub Desktop.
Save kriegsman/88954aae22b03a664081 to your computer and use it in GitHub Desktop.
MapTwinkle- randomly twinkle pixels up from a base color to a peak color and back down.
#include "FastLED.h"
// MapTwinkle
// Designed to illuminate a 'map' of pixels, each of which randomly
// sometimes twinkles brighter and then back down to it's base color again.
//
// Parameters include: background color, peak twinkle color, and speed
// of brightening and dimming.
//
// Mark Kriegsman, August 2015
#define LED_PIN 13
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define MASTER_BRIGHTNESS 255
// Base background color
#define BASE_COLOR CRGB(32,0,32)
// Peak color to twinkle up to
#define PEAK_COLOR CRGB(64,0,64)
// Currently set to brighten up a bit faster than it dims down,
// but this can be adjusted.
// Amount to increment the color by each loop as it gets brighter:
#define DELTA_COLOR_UP CRGB(4,0,4)
// Amount to decrement the color by each loop as it gets dimmer:
#define DELTA_COLOR_DOWN CRGB(1,0,1)
// Chance of each pixel starting to brighten up.
// 1 or 2 = a few brightening pixels at a time.
// 10 = lots of pixels brightening at a time.
#define CHANCE_OF_TWINKLE 1
void setup() {
delay(3000);
FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(MASTER_BRIGHTNESS);
InitPixelStates();
}
void loop()
{
TwinkleMapPixels();
FastLED.show();
FastLED.delay(20);
}
enum { SteadyDim, GettingBrighter, GettingDimmerAgain };
uint8_t PixelState[NUM_LEDS];
void InitPixelStates()
{
memset( PixelState, sizeof(PixelState), SteadyDim); // initialize all the pixels to SteadyDim.
fill_solid( leds, NUM_LEDS, BASE_COLOR);
}
void TwinkleMapPixels()
{
for( uint16_t i = 0; i < NUM_LEDS; i++) {
if( PixelState[i] == SteadyDim) {
// this pixels is currently: SteadyDim
// so we randomly consider making it start getting brighter
if( random8() < CHANCE_OF_TWINKLE) {
PixelState[i] = GettingBrighter;
}
} else if( PixelState[i] == GettingBrighter ) {
// this pixels is currently: GettingBrighter
// so if it's at peak color, switch it to getting dimmer again
if( leds[i] >= PEAK_COLOR ) {
PixelState[i] = GettingDimmerAgain;
} else {
// otherwise, just keep brightening it:
leds[i] += DELTA_COLOR_UP;
}
} else { // getting dimmer again
// this pixels is currently: GettingDimmerAgain
// so if it's back to base color, switch it to steady dim
if( leds[i] <= BASE_COLOR ) {
leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
PixelState[i] = SteadyDim;
} else {
// otherwise, just keep dimming it down:
leds[i] -= DELTA_COLOR_DOWN;
}
}
}
}
@climent
Copy link

climent commented Oct 10, 2016

Are you using memset() correctly in line 62? All the references point at 2nd and 3rd have been transposed.

memset (var, value, sizeof(var));

@hawkiee552
Copy link

hawkiee552 commented Aug 24, 2017

Thanks man, this really helped creating this soft warm-white twinkle to my upcoming christmas light project.

One question, I'm trying to do a version where it goes from warm white and "brightens up" to completely red (no green from the base color). I can't seem to do this properly. So basically a warm white base with red twinkling.

What I've done so far is to edit these RGB values and increments.

`
#define BASE_COLOR CRGB(128,32,0)

#define PEAK_COLOR CRGB(255,32,0)

#define DELTA_COLOR_UP CRGB(5,1,0)

#define DELTA_COLOR_DOWN CRGB(5,1,0)
`

I were able to get full red color in the twinkling, but it wouldn't dim down and just snap to the base color. This was with the decrement off (which was the problem).

Thanks again, you saved me countless hours and frustration as this is exactly the code I was looking after.

@systembolaget
Copy link

systembolaget commented Oct 9, 2018

This is the one and only random shimmer/flicker effect done with FastLED that holds what it promises, thanks for making it available.

The question is, however, how to integrate this behaviour with a basic effect; for example when the entire strip of LEDs is very slowly changed by fetching colours from a palette. In that case, there is no base colour and peak colour, but rather one would only darken/brighten random LEDs randomly with the principle shown above.

Any ideas?

ps: when FastLED.delay(20); is prolonged to a second or even more, for a nice cloud-like effect, the darkening/brightening becomes very "steppy". Is there a method to avoid that?

@BautiCussi
Copy link

Hi, do you know how to adapt this to be used with a switch and set different colours in different cases? or as an if function. Thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment