Create code for a semi random FastLED pattern

random(min,max); // excludes max:

Edit: Nevermind.

I do not know if Wokwi or "fade to black" is doing it, but red seems to have leftovers. Maybe a hand-made, proportional, color fade would be good.

I think neopixel.fadeToBlackBy is just a brain-dead decrement.

And Wokwi-wise, attempting color correction of browser rendered simulation of images of LEDs will never work out well.

it seems to work OK on my Mac with Safari but in general wokwi does not do so well with strips and lighting so I would not trust what you see in the simulation... Would be interesting to know if it works in real life :slight_smile:

it is indeed quite "simple". I specify the fadeBy decrement to be 1 in the call.

void nscale8( CRGB* leds, uint16_t num_leds, uint8_t scale)
{
    for( uint16_t i = 0; i < num_leds; ++i) {
        leds[i].nscale8( scale);
    }
}

void fadeToBlackBy( CRGB* leds, uint16_t num_leds, uint8_t fadeBy)
{
    nscale8( leds, num_leds, 255 - fadeBy);
}

(there is a bit of math in nscale8 but not much)

It works! Thank you so much :slight_smile:

@sundancekid - Question fom themasses: When the fade from yellow to black is near black, do you still see yellow or is red predominant?

it's amber not yellow :slight_smile:

glad if that helped, make sure you study the code and state machines if you want to make progress...

It was fading from amber to dark red. Ive since changed the amber color to white which then fades to off. Works much better!

1 Like

Thanks again JML! Just wondering if its possible to fade in at the beginning as I think it looks a bit harsh without. Thanks.

I'm not sure what you mean but it's likely possible :wink:

if you understood the state machine approach then you should be able to adjust the code to your needs. Give it a try.

1 Like

The fade out is perfect. Was just wondering if I could do a fade in at the start of the loop?

sure you can. there is no built in fade in to a given color (just the fade out to black) so you'll have to play with strip's brightness or work in the HSV space and play with the V to increase the perceived brightness of the color during the fade in step (which you will have to add into the sequence)

Since @J-M-L's code uses a state machine, it goes through the beginning of loop(){...} millions of times. You can add some FADE_IN code as an additional state.

Cool thanks!

Ok - will have a play around with it. Thanks DaveX!

OK - it's my good night gift

here is a new version using the strip brightness on the way in and out.

it could possibly fix the red-ish issue as I think the brightness is gamma compensated.

have fun

click to see the code
/* ============================================
  code is placed under the MIT license
  Copyright (c) 2024 J-M-L
  For the Arduino Forum : https://forum.arduino.cc/u/j-m-l

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  ===============================================
*/



#include <FastLED.h>

const byte stripPin = 2;
const byte numLeds = 100;
const byte numChosenLeds = 30;
const CRGB amber = CRGB(255, 191, 0);

CRGB leds[numLeds];
byte indexArray[numLeds];

enum {SHUFFLE, FADEIN, WAIT, FADEOUT} state = SHUFFLE;
const unsigned long waitTime = 2000ul; // make it 20000ul for 20s, limiting at 2s for demo
const unsigned long fadeStepTime = 10ul;
unsigned long startTime;
byte brightness;

void fisherYatesShuffle() {
  for (size_t i = numLeds - 1; i > 0; --i) {
    size_t r = random(0, i + 1); // generate rand numbers from 0 to i
    size_t tmpSwap = indexArray[i];
    indexArray[i] = indexArray[r];
    indexArray[r] = tmpSwap;
  }
}

void animateStrip() {
  switch (state) {
    case SHUFFLE:
      fisherYatesShuffle();
      FastLED.clear();
      FastLED.show();
      for (byte i = 0; i < numChosenLeds; i++)leds[indexArray[i]] = amber; // turn the selected ones amber
      brightness = 0;
      state = FADEIN;
      break;

    case FADEIN:
      if (millis() - startTime >= fadeStepTime) {
        FastLED.setBrightness(++brightness); 
        FastLED.show();
        startTime = millis();
        if (brightness == 255) state = WAIT;
      }
      break;

    case WAIT:
      if (millis() - startTime >= waitTime) {
        startTime = millis();
        state = FADEOUT;
      }
      break;

    case FADEOUT:
      if (millis() - startTime >= fadeStepTime) {
        FastLED.setBrightness(--brightness);
        FastLED.show();
        startTime = millis();
        if (brightness == 0) state = SHUFFLE;
      }
      break;
  }
}



void setup() {
  randomSeed(analogRead(A0));
  Serial.begin(115200);
  for (byte i = 0; i < numLeds; i++) indexArray[i] = i;
  FastLED.addLeds<WS2812B, stripPin, GRB>(leds, numLeds);
}

void loop() {
  animateStrip();
  // you can do other stuff here as long as it does not take too long
  // ....
}
3 Likes

Your a legend man. Works like a dream. Many thanks!!!!