Twinkle effect acting only on specific LED positions

Wondering if anyone has run across code that allows for a "twinkling" effect (similar to Mark Kriegsman's - MapTwinkle) that I can assign to led positions in an array. I thought I could use something similar to his code, but reading more about fill_solid has be worried it is not intended for spaces between leds. I was hoping to get that twinkle effect on an array. Specifically only these leds in my sketch would twinkle.

int orange[11] = {0,3,6,9,12,15,18,21,24,27,30};

Not sure if it is important or not, but I do plan to have the other led positions running a different function.

This is Mark's code I was looking at for inspiration

#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     2
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS    90
CRGB leds[NUM_LEDS];

#define MASTER_BRIGHTNESS   100

// Base background color
#define BASE_COLOR       CRGB(22,0,22)

// Peak color to twinkle up to
#define PEAK_COLOR       CRGB(84,0,84)


// 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 4

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(30);
}

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;
      }
    }
  }
}

Any suggestions would be appreciated by this rookie.

add a state for your pixels that should not be impacted

const int twinklingPixels[] = {0,3,6,9,12,15,18,21,24,27,30};
const size_t twinklingPixelsCnt = sizeof twinklingPixels / sizeof * twinklingPixels;

enum { NotTwinkling, SteadyDim, GettingBrighter, GettingDimmerAgain };
uint8_t PixelState[NUM_LEDS];

void InitPixelStates()
{
  // initialize all the pixels to NotTwinkling  and to base color
  for (int ledIndex = 0; ledIndex < NUM_LEDS; ledIndex++) {
     PixelState[twinkleIndex] =  NotTwinkling; 
     leds[i] = BASE_COLOR; 
  }

  // mark then only the relevant ones to be SteadyDim.
  for (int twinkleIndex = 0; twinkleIndex < twinklingPixelsCnt; twinkleIndex ++) {
      PixelState[twinklingPixels[twinkleIndex] ] =  SteadyDim;  
      // leds[twinklingPixels[twinkleIndex] ] = XXX;  // set a different color for those if needed
  }

  FastLED.show();
}

then when you call TwinkleMapPixels() those pixels with the NotTwinkling flag won't be taken into account.

Twinkle only ranges of orange, or orange to magenta?

In this adaptation, I added an orange twinkle using the pixels in the array. I outlined the three sections I changed in asterisks.

sketch.ino
#include "FastLED.h"

#define LED_PIN     2
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS    90
CRGB leds[NUM_LEDS];

#define MASTER_BRIGHTNESS   255
#define BASE_COLOR          CRGB(63,0,63)
#define PEAK_COLOR          CRGB(255,0,255)

// 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)

// 1 or 2 = a few brightening pixels at a time.
// 10 = lots of pixels brightening at a time.
#define CHANCE_OF_TWINKLE 4

/************************************************************/
int orange[] = {0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30};
unsigned long timer, timeout = 250; // for twinkle
/************************************************************/

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();
/************************************************************/
  if (millis() - timer > timeout) {
    timer = millis();
    twinkle();
  }
/************************************************************/
  FastLED.delay(30);
}

/************************************************************/
void twinkle() {
  int twink = random(11); // from the array
  leds[orange[twink]] = CRGB(255, 170, 0); // orange
  FastLED.show();
  FastLED.delay(30);
  leds[orange[twink]] = CRGB(255, 0, 255); // magenta
}
/************************************************************/

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;
      }
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.