0

Hello help needed trying to get fastLED to start and stop at specific points on the strip, i have this nice fade animation but need to split the strip intov about 3 sections and have it run at different times.

here is the mess i have made any help welcome...

#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 6 //How boring and obvious!
#define COLOR_ORDER GRB //Green (G), Red (R), Blue (B)
#define CHIPSET WS2812B
#define BRIGHTNESS 60
#define VOLTS 5
#define MAX_AMPS 500 //value in milliamps

//ENOUGH NONSENSE!

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<CHIPSET,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
  FastLED.show(); 
}

void loop() {
  fadeToBlackBy(leds, NUM_LEDS, 4);      // Smaller value = longer tail
  int i = (millis()/40) % NUM_LEDS;      // How fast it goes.
  leds[i] = CRGB::White;
  FastLED.show();
}

Cheers Rob

4
  • Any idea how i can set a start led and end led position that is not the full lengh of the strip? my micky mouse version ... for(int i=40; i<strip.numPixels() -43; i++) { // For each pixel in strip... i edit the "i=40" bit then the "-43;"
    – Acewindel
    Commented Apr 16, 2023 at 6:49
  • do you realize that you can write code like this? ... for(int i=start; i<end; i++)
    – jsotola
    Commented Apr 16, 2023 at 7:00
  • Ok very sorry first time on here ill edit the question.
    – Acewindel
    Commented Apr 16, 2023 at 7:37
  • how about something like this wokwi.com/projects/362275278547275777
    – jsotola
    Commented Apr 17, 2023 at 19:20

1 Answer 1

2

Here is an example sketch that shows how to manipulate data in a sub-section of an array.

https://wokwi.com/projects/362275278547275777

The important thing to understand, is that the FastLED.show(); is the function that does the sending of the leds[] array contents to the physical LED strip.

That means that you can do anything you want to the leds[] array, and the changes will not be visible until the show() function is called.

You can make changes in bits and pieces, without seeing the change increments.

This sketch also shows how to use timers to synchronize actions


#include <FastLED.h>

#define CHIPSET     WS2812B
#define COLOR_ORDER     GRB         // Green (G), Red (R), Blue (B)
#define NUM_LEDS         95
#define DATA_PIN          3
#define BRIGHTNESS      255
#define VOLTS             5
#define MAX_AMPS        500         // value in milliamps

CRGB leds[NUM_LEDS];                // main LED array
CRGB test[10];                      // set up separate array for merging with main LED array
                                    // kind of a sprite
int indx = 0;

boolean tick[4] = {false, false, false, false};    // countdown timers flags

unsigned long timeInterval  [4] = {10, 20, 1, 2};  // timers load with these values
unsigned long timeCounter   [4];                   // these are the actual timers

unsigned long previousMicros = 0;


void setup() {

  Serial.begin(115200);

  FastLED.addLeds<CHIPSET,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();

  memcpy(timeCounter, timeInterval, 4 * sizeof(long));  // preload timers
}

void loop() {

  unsigned long currentMicros = micros();

  if (currentMicros - previousMicros >= 1000) {   // 1 ms tick
    for (int i = 0; i < 4; i++) {
      previousMicros = currentMicros;

      timeCounter[i]-- ;                          // keep track of 4 time intervals
      if (timeCounter[i] == 0) {
        timeCounter[i] = timeInterval[i];         // reached zero, reload counter and emit a "tick"
        tick[i] = true;
      }
    }

    // fade sections of leds[] array at different rates
    // NOTE: "&leds[0]" and "leds" both point to the beginning of the array
    fadeToBlackBy(&leds [0], 30,  3);               //  long tail
    fadeToBlackBy(&leds[30], 30, 10);               //   mid tail
    fadeToBlackBy(&leds[60], 30, 20);               // short tail
    fadeToBlackBy(&leds[90],  5,  1);               // really long tail

    if (tick[0]) {    // 10 ms tick
      tick[0] = false;
      leds[indx] = CRGB::White;                   // add the "running dot" after fades are applied
      indx++;
      if (indx > (NUM_LEDS-1)) indx = 0;
    }

    if (tick[1]) {    // 20 ms tick
      tick[1] = false;

//    the &test[0] points to the array element directly
//    it allows the choice of a starting point for the fade function

//    fadeToBlackBy( test,    3, 16);               // these two lines are the same
      fadeToBlackBy(&test[0], 3, 16);               // fade only first three elements in the array
      fadeToBlackBy(&test[3], 1, 20);               // fade only the third element in the array

      if(test[0].r < 64){                           // pixels faded enough
        test[0]= CRGB(248, 11, 130);                // reload pixels
        test[1]= CRGB(221, 245, 6);
        test[2]= CRGB(112, 16, 236);
        test[3]= CRGB(16, 236, 53);
      }
    }
    memcpy(&leds[10], test,     4 * sizeof(CRGB));    // apply 4 pixels to leds[] array
    memcpy(&leds[50], &test[0], 4 * sizeof(CRGB));    // apply 4 pixels to leds[] array
    memcpy(&leds[80], &test[1], 3 * sizeof(CRGB));    // apply 3 pixels to leds[] array

    FastLED.show();                                   // send pixel data to the LED strip
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.