1

I'm using FastLED library, Arduino Nano and 5 addressable LEDs (WS2812B). I'd like to create the sequence in the image attached. (all in white color for example)

I know how to turn ON each LED every 0.2 sec and I know how to fade. I don't know how to make sure each LED goes from 0% brightness to 100% in 0.4 sec + start fading in the next LED after 0.2 sec.

Here is a fading LED:

void loop() {
  uint32_t cur_ms = millis();
    if (cur_ms - fade_ms > 200) {
        fade_ms = cur_ms;
        brightness += 5;
        leds[0] = CHSV(255, 0, brightness);
    }

FastLED.show();
// delay(10);

}

Can anyone help me figure it out? enter image description here

3
  • Hi Welcome to ASE. Just an Idea. You can use the remaining millis() and the current brightness value to compute the next needed jump or reduction in the brightness
    – ArduinoFan
    Jul 26 '21 at 12:27
  • I'd appreciate if you could supply a snippet
    – yuvix_man
    Jul 26 '21 at 13:00
  • can you share the complete code snippet of what you have worked on? Also, The below answer is really really good. You might be in a hurry now, but bookmark it and come back when you have time.
    – ArduinoFan
    Jul 27 '21 at 15:15
1

One of the SimpleTimer libraries such as this one by Marcello Romani (which I mention because I have used it; there are others). It creates a timers object that has 10 (in this case) software-based timers with a variety of operating modes (run once, run x times, run forever, e.g.).

The basic idea (for a 1-LED example) is that you write a function (known as a "callback" to step up the brightness of an LED by one step. Let's call it stepUp().

You write another callback function to turn off the LED and start a timer that will call stepUp() at whatever interval you choose for as many times as it takes to reach 100%. Let's call this one cycleLED().

In your setup(), you'll start another timer that will call cycleLED() as often as you want that process to run - every 1200 mSec in this case - and for as many times (or forever) as you choose.

In your loop() function, you'll need a call to your timer object's .run() function as often as possible - that is what makes the timers work, and it is the timers that will call your callback functions to make your LEDs behave as you want them to.

So far, we one fading-up LED, and I'd recommend you implement this much at first.

There are two ways to go from here. You can write the above pair of callbacks for each LED, but they will be nearly identical and why write maintain the same code, several times over? Or you can use another feature of the library - a pointer-to-void argument to the callback function - in which you can pass any value, such as an LED's number or address, so that one callback could operate on the specified LED.

If you go with first option, your setup() function will need to to start 5 timers, one for each cycleLED() function (which would need to have distinct names, of course), and those timers start-times will need to be staggered.

With the second option, you would write a third callback, allCycles() that sequences the starts of each cycleLED() in turn. In this case, your setup() function would only need to start a timer to repeatedly call allCycles().

There's a lot there, if these are new concepts. If so, it will be a lot easier to experiment with the SimpleTimer library doing something trivial at first, like flashing (not fading) one LED, then several LEDs, until this way of coding timed events feels less foreign.

3
  • Thank you, but this isn't helpful. It should be very simple & clean code. I should use this: "if (cur_ms - x_ms > y_ms)" twice. One for incrementing the brightness and one for increasing the index to the next LED. I also need a struct/array to create different brightness value for each LED.
    – yuvix_man
    Jul 26 '21 at 13:15
  • 1
    It really doesn't get much cleaner than that. You can of course, write layers of if (cur_ms - x_ms > y_ms) and keep all of the code visible all of the time, but modular code is generally easier to understand, write correctly, debug, and maintain. You're welcome.
    – JRobert
    Jul 26 '21 at 13:23
  • @yuvix_man how is Jroberts answer not helpful? Jrobert laid out everything clearly. Writing actual (clean) code is up to you, which should be easy after this explanation.
    – StarCat
    Jul 27 '21 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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