XRAD'S fastLED FastLED.clear(); question

I have this:

CRGB leds[NUM_LEDS];
CRGB leds2[NUM_HEADLIGHTS];


FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE, HEADLIGHTS, COLOR_ORDER  (leds2,NUM_HEADLIGHTS). setCorrection(TypicalLEDStrip);

But I can't find a way to just FastLED.clear() first set of (leds).... without clearing the second....(leds2)

here is my whole code

#include <FastLED.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
//#include <Adafruit_NeoPixel.h>


Adafruit_LIS3DH lis = Adafruit_LIS3DH();//G-force sensor


#define NUM_LEDS 26
#define LEDSEGMENT 3 //segment length of lit pixels
CRGB leds[NUM_LEDS];
//CRGB color;


#define HEADLIGHTS 10   //headlights
#define NUM_HEADLIGHTS 2
CRGB leds2[NUM_HEADLIGHTS];




CRGB color[] = {CRGB::Green, CRGB::Orange, CRGB::White,
                CRGB::Violet, CRGB::LightGreen, CRGB::DarkRed,
                CRGB::MediumAquamarine, CRGB::SkyBlue
               };






#define DATA_PIN 9      //neopixel pin data front/rear turn signals
#define BRIGHTNESS  100  //neopixel brightness


// neopixel variables
int LEDValue;
int LEDColor;
int LEDRun;



#define COLOR_ORDER GRB
#define LED_TYPE WS2812B






int colorChoice = 0;


//assign LED pins
#define FRONTGREENLED 11
#define REARREDLED    12




//non blocking PWM led controls
int LedShow = 100;     // runs through led fade 'if' statement 'x' times
int brightness = 0;    // initial brightness of turn signal leds (0-255)
int fadeAmount = 3;    //fade by this much
bool runFlag = false;  //flag T/F for motion sensor 'z'
int ledRunCount;      //keeps track of how many ledShow runs
unsigned long previousMillis = 0; //store last Led update
int interval = 50;               //fade at this interval






void ALLLEDSON() {
  analogWrite(FRONTGREENLED, 150);
  analogWrite(REARREDLED, 150);
}










void setup() {
   Serial.begin(115200);
   delay(2000);


  lis.begin(0x18);
  lis.setRange(LIS3DH_RANGE_2_G);   // 2, 4, 8 or 16 G!
  lis.setDataRate(LIS3DH_DATARATE_50_HZ);//1,10,25,50,100,200,400




  randomSeed(analogRead(23));


  pinMode(HEADLIGHTS, OUTPUT);
  pinMode(FRONTGREENLED, OUTPUT);
  pinMode(REARREDLED, OUTPUT);


  ALLLEDSON();//turn on F and R leds




  // initiate the LED strip
  //FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  //FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);


  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);


  FastLED.addLeds<LED_TYPE, HEADLIGHTS, COLOR_ORDER>(leds2, NUM_HEADLIGHTS).setCorrection(TypicalLEDStrip);




  FastLED.setBrightness( BRIGHTNESS );


  colorChoice = 0;


   for (int x = 0; x < NUM_HEADLIGHTS; x++) {
    leds2[x] = color[6];
   }
  FastLED.show();
  
  delay(3000);
}


void loop() {


  unsigned long currentMillis = millis();


 /* //[u]I want these to stay lit[/u]
    for (int x = 0; x < NUM_HEADLIGHTS; x++) {
      leds2[x] = color[3];
       FastLED.show();
    }
*/


  //get a new sensor event, normalized
  sensors_event_t event;
  lis.getEvent(&event);




  // Map function here
  LEDValue = map(event.acceleration.x, -18, 17, 0, NUM_LEDS);//have to adjust a bit to center
  LEDColor = map(event.acceleration.y, -10, 10, 0, 10);//adjust to change color
  LEDRun = map (event.acceleration.z, 0, 20, 0, 10);




  if (LEDColor < 4 || LEDColor > 6) {
    colorChoice = colorChoice + 1;
    if (colorChoice > 7) {
      colorChoice = 0;
    }
  }




  FastLED.clear();//clears ALL leds on both strips


  if (LEDValue <= LEDSEGMENT) {

    for (int led = 0; led <= LEDValue; led++) {
      leds[led] = color[colorChoice];
    }
    FastLED.show();
  }


  else {
  
    for (int led = LEDValue - LEDSEGMENT; led < LEDValue; led++) {
      leds[led] = color[colorChoice];
    }
    FastLED.show();
  }


  if (LEDRun < 4 || LEDRun > 6) {
    runFlag = true;
  }
  if (runFlag == true) {


    for (int i = 0; i < LedShow; i++) {
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;


        
        analogWrite(FRONTGREENLED, random(brightness));
        analogWrite(REARREDLED, random(brightness));
        //for (int i = 0; i < LedShow; i++) {
        brightness = brightness + fadeAmount;
        if (brightness <= 0 || brightness >= 150) {
          fadeAmount = -fadeAmount;
        }
        ledRunCount ++;
        if (ledRunCount == LedShow ) {
          runFlag = false;
          ledRunCount = 0;
          analogWrite(FRONTGREENLED, LOW);
          analogWrite(REARREDLED, LOW);
        }
      }
    }
  }
  else {
    ALLLEDSON();
     
  }
  //Serial.println(event.acceleration.y);
  //Serial.println(LEDRun);
}//end loop










//Serial.println(event.acceleration.x);//uncomment one at a time to test
//Serial.println(LEDValue);
//Serial.println(event.acceleration.y);
//Serial.println(LEDColor);
//Serial.println(event.acceleration.z);
//Serial.println(LEDRun);
//Serial.println(runFlag);
//Serial.println(ledRunCount);

Hello,

Use a for loop like you do here

for (int x = 0; x < NUM_HEADLIGHTS; x++) {
    leds2[x] = color[6];
   }
  FastLED.show();

just with black color

Or, if you look at the FastLED documentation, you can see addLeds returns a CLedController object

Thx for reply! I should have mentioned that I already tried r,g,b, in 0,0,0. created nasty flicker, worse flicker than clear().....because i'm always clearing and writing every loop...at least for the HEADLIGHTS...but i will try it for the other strip when I get home...

problem is that clear(), even in a function outside loop, is not limited to just clearing the functions leds, but ALL strips...

thought I read somewhere that there was a way to limit FastLED clear(), can't find it now..something like FastLED clear(leds).....this doesn't work obvously....

i will read about the 'controller class' tonight too!

Did you read the documentation for CLEDController that @guix linked in Reply #1? I'd look at calling either the clearLeds() or clearLedData () function on the specific CLEDController.

clearLedData()...that's what I couldn't find! Thx!

Sorry, not getting it through some experiments and googling. Yet.

How are two strips created?

What is the exact syntax of "show" or "clear" when we are applying them to just one strip?

The fastLED calls look global, I assume there is a way to apply them here or there.

TIA

a7

Forgive these snippets, just check me on this, so:

To make two strips and bind them to LED type, storage and output pin:

CRGB lamps[NLAMPS];
CRGB morelamps[NLAMPS];

CFastLED ONE, TWO;

void initLamps()
{
	ONE.addLeds<APA102,DATA_PIN, CLOCK_PIN,BGR>(lamps, NLAMPS);
	TWO.addLeds<APA102,OTHER_DATA, CLOCK_PIN,BGR>(morelamps, NLAMPS);
}

and this will "show" both strips but makes it obvious how one might show only one or the other., I see the pattern, just wanna be sure I am on the right track here:

void lampsShow()
{
	ONE.show();
	TWO.show();
}

?

a7

Strips are created and registered with FastLED by the addLeds function which returns a reference to a CLedController object. The clearLedData() function can be called on individual strips. FastLED.show() writes the data out to all registered strips. Note that only FastLED.show() actually writes to the LED strips and causes their colors to change. All other functions only modify the data stored in the CRGB arrays. Nothing is visible on the LEDs until FastLED.show() is called.

Using References:

#include <FastLED.h>

#define COLOR_ORDER GRB
#define LED_TYPE WS2812B

#define DATA_PIN 9
#define NUM_LEDS 26
CRGB leds[NUM_LEDS];

#define HEADLIGHTS 10   //headlight led pin
#define NUM_HEADLIGHTS 2
CRGB leds2[NUM_HEADLIGHTS];

CLEDController &ledController = FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
CLEDController &headlightController =   FastLED.addLeds<LED_TYPE, HEADLIGHTS, COLOR_ORDER>(leds2, NUM_HEADLIGHTS);

void setup() {
  ledController.setCorrection(TypicalLEDStrip);
  headlightController.setCorrection(TypicalLEDStrip);

  ledController.clearLedData();
  headlightController.clearLedData();
  FastLED.show();
}

void loop() {
}

Using Pointers:

#include <FastLED.h>

#define COLOR_ORDER GRB
#define LED_TYPE WS2812B

#define DATA_PIN 9
#define NUM_LEDS 26
CRGB leds[NUM_LEDS];

#define HEADLIGHTS 10   //headlight led pin
#define NUM_HEADLIGHTS 2
CRGB leds2[NUM_HEADLIGHTS];

CLEDController *ledController;
CLEDController *headlightController;

void setup() {
  ledController = &(FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS));
  headlightController = &(FastLED.addLeds<LED_TYPE, HEADLIGHTS, COLOR_ORDER>(leds2, NUM_HEADLIGHTS));

  ledController->setCorrection(TypicalLEDStrip);
  headlightController->setCorrection(TypicalLEDStrip);

  ledController->clearLedData();
  headlightController->clearLedData();
  FastLED.show();
}

void loop() {
}

Both compile but are untested.

EDIT:
Looking at the source code, I see that CLEDController also provide a show() function so you can apply it to individual strips rather than using FastLED.show() for all strips.

alto777:
Forgive these snippets, just check me on this, so:

To make two strips and bind them to LED type, storage and output pin:

CFastLED ONE, TWO;

I don't know if it's OK to make multiple CFastLED objects or if the one named FastLED is meant to be a singleton. That's why I chose in my code to instantiate multiple CLEDController objects all under control of the FastLED instance.

@gfvalvo: Then please elaborate a bit, still not seeing how you (syntax-wise)

"can apply [show()] to individual strips rather than using FastLED.show() for all strips."

Seems like you'd be kinda limited if you couldn't, so how do you express

a. create multiple independent strips
b. apply show(), for example, to one of them only

My brief glance at the source code led me to think there is a global chain of strips, so show() shows them all, but separate strips can be addressed, so to speak, at will.

I'm just winging it after finding the definition of LEDS.

My example compiles with the proper context, but I have not run it. I suppose the next thing is to do that but it involves gathering some hardware, also it is nap time.

a7

Looking at the CLEDController class source code again, I it looks like the proper member function would be showLeds(). Here's the code from controller.h:

    /// show function using the "attached to this controller" led data
    void showLeds(uint8_t brightness=255) {
        show(m_Data, m_nLeds, getAdjustment(brightness));
    }

So, using a pointer:

ledController->showLeds();

Using a reference:

ledController.showLeds();

When you add multiple strips of LEDS, FastLED can be indexed

  FastLED.addLeds<WS2812, LEFT_HEADLIGHT_PIN, GRB>(leds, NUM_HEADLIGHT_LEDS); // left headlights (4)
  FastLED.addLeds<WS2812, RIGHT_HEADLIGHT_PIN, GRB>(leds, NUM_HEADLIGHT_LEDS); // right headlights (4)
  FastLED.addLeds<WS2812, ROOF_LIGHT_PIN, GRB>(leds, NUM_ROOF_LEDS); // roof lights (2)
// this enum must be the same as the template order above
// so we can use them as in index into the array
enum headlights { LEFT_HEADLIGHT, RIGHT_HEADLIGHT, ROOF_LIGHT };

....and then later...

      FastLED[ROOF_LIGHT].showLeds(gBrightness);
      // or
      FastLED[RIGHT_HEADLIGHT].showLeds(gBrightness);

You will notice that this code only has 1 actual array of leds and the buffer is recycled for each strip I want to use. The downside of this approach is that you have to call the showLeds() with the global brightness you want rather than show(). I believe I based this code on the example code from the library.

blh64:
When you add multiple strips of LEDS, FastLED can be indexed

It's the same thing. The CFastLED class overloads operator[] to return a reference to a CLEDController:

	/// Get a reference to a registered controller
	/// @returns a reference to the Nth controller
	CLEDController & operator[](int x);

So, you can either index with [] or use the reference / pointer provided by addLeds(). Either way, you get to the same CLEDController instance.

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