Changing colors and fading for specific time with millis

Hi everyone.
I am trying to have a specific sequence of colors changing on my led strip one after another with each color having different duration time. Also I want to do this with millis cause I will integrate it in bigger code and I don't want it to be frozen because of delay. I have managed to do transition with brightness staying same all the time and it works fine (code below):

#include <FastLED.h>

#define NUM_LEDS 20
#define DATA_PIN 5
#define BRIGHTNESS 127

CRGB leds[NUM_LEDS];

const unsigned long period_1 = 1000;
const unsigned long period_2 = 2000;
const unsigned long period_3 = 3000;
unsigned long currentTime = 0;
unsigned long prevTime = 0;
int color = 0;




void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  Serial.begin(9600);
}

void loop() {

  currentTime = millis();
  
  switch (color) {

  case 0:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(9, 151, 95);}
    FastLED.show();
    for(brightness = 0; brightness<=130; brightness+fadeAmount)
    {
    } 
       
    
    if(currentTime - prevTime >= period_2)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 1:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(0, 255, 50);}
  FastLED.show();
  if(currentTime - prevTime >= period_1)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 2:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(95, 158, 200);}
    FastLED.show();
    if(currentTime - prevTime >= period_3)
    {
      prevTime = currentTime;
      color++;
    }  
  break;

  case 3:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(100, 157, 240);}
    FastLED.show();
    if(currentTime - prevTime >= period_2)
    {
      prevTime = currentTime;
      color++;
    } 
  break;

  case 4:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(65, 166, 179);}
    FastLED.show();
    if(currentTime - prevTime >= period_1)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 5:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(100, 149, 237);}
    FastLED.show();
    if(currentTime - prevTime >= period_3)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 6:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(0, 71, 171);}
    FastLED.show();
    if(currentTime - prevTime >= period_2)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 7:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(20, 52, 164);}
    FastLED.show();
    if(currentTime - prevTime >= period_1)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 8:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(128, 0, 128);}
  FastLED.show();
  if(currentTime - prevTime >= period_3)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 9:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(170, 51, 106);}
  FastLED.show();
  if(currentTime - prevTime >= period_2)
    {
      prevTime = currentTime;
      color++;
    }
  break;
  
  case 10:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(103, 49, 71);}
  FastLED.show();
  if(currentTime - prevTime >= period_1)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 11:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(159, 43, 104);}
  FastLED.show();
  if(currentTime - prevTime >= period_3)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 12:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(75, 0, 130);}
  FastLED.show();
  if(currentTime - prevTime >= period_2)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 13:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(93, 63, 211);}
  FastLED.show();
  if(currentTime - prevTime >= period_1)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 14:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(170, 51, 106);}
  FastLED.show();
  if(currentTime - prevTime >= period_3)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 15:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(251, 150, 50);}
  FastLED.show();
  if(currentTime - prevTime >= period_2)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 16:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(255, 255, 0);}
  FastLED.show();
  if(currentTime - prevTime >= period_1)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 17:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(255, 234, 0);}
  FastLED.show();
  if(currentTime - prevTime >= period_3)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 18:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(228, 208, 10);}
  FastLED.show();
  if(currentTime - prevTime >= period_2)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 19:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(223, 255, 0);}
  FastLED.show();
  if(currentTime - prevTime >= period_1)
    {
      prevTime = currentTime;
      color++;
    }
  break;

  case 20:
    color=0;
  break;
  }
}                
           

So now I want to have smoother transitions between colors with sequence as follows: color1 fades in from 0 to 130 brightness, stays at 130 brightness for some period of time (period 1,2 or 3), color1 fades out to 0 brightness, color2 fades in from 0 to 130 brightness, etc. I have managed to figure out how to fade in and out single color using delay (code below) but like I mentioned earlier I dont want to use delay cause it freezes code. I am having trouble putting in fade effect using millis. I would have same steps of fading (5) for same amount of times between them (30ms).

#include "FastLED.h"
#define NUM_LEDS 20
CRGB leds[NUM_LEDS];
#define PIN 5
int fadeAmount = 5;  // Set the step of fade.
int brightness = 0; 

void setup()
{
  FastLED.addLeds<NEOPIXEL, PIN>(leds, NUM_LEDS);
}

void loop()
{ 
   for(int i = 0; i < NUM_LEDS; i++ )
   {
   leds[i].setRGB(0,255,250);
   leds[i].fadeLightBy(brightness);
  }
  FastLED.show();
  brightness = brightness + fadeAmount;
  if(brightness == 0 || brightness == 130)
  {
    fadeAmount = -fadeAmount ; 
  }    
  delay(30);  // This delay sets speed of the fade.
}

Could anyone help me out to figure this out?
Thanks in advance

Sure, the whole idea of your code is wrong.
You need to implement a state machine. This involves removing removing all the delays and for loops.

This gets asked at least every week. Do a search for several things at once or see the blink without delay in the examples section of the IDE.

I have posted examples of this for addressable LEDs many times, so do a forum search.

2 Likes

As you can see in my first part of code posted above I have already implemented thing similar to blink without delay in that part of code. The trouble I am having now is adding fade effects with millis into that. I will look into state machine to see how to implement it in the code.

Agreed with @Grumpy_Mike , the structure of your first code is wrong.

Look at the code - you go into the switch, select the case, assign a color to the entire strip, and only then check if the period has ended. Thus, while the period is running. you assign the same color to the leds dozens of times. For what?
It would be more correct to first check the time and only if the period has already ended - change the color:

void loop() {

  currentTime = millis();
  
  if(currentTime - prevTime >= interval)
    {
      prevTime = currentTime;
      color++;
      switch (color) {
     
      case 1:
       for(int i=0;i<NUM_LEDS;i++){
       leds[i].setRGB(0, 255, 50);}
       FastLED.show();
       interval = period1;
      break;

      case 2:
       for(int i=0;i<NUM_LEDS;i++){
       leds[i].setRGB(0, 255, 50);}
       FastLED.show();
       interval = period2;
      break;

    .....

   } // switch
 } //   if(currentTime - prevTime >= interval)

  

This code is a some kind of finite state machine already :slight_smile:

1 Like

No you haven't.

You seem to have not understood the concept. The code should return to the loop that checks the time to see if it is time to go into the functions again.

1 Like

Ok I think Im understanding it now, from your code, basically putting millis outside to encapsulate switch case loop and then setting interval for each case in itself. For some reason first code that I posted worked just fine for me using it into complete code even when I pressed button that was supposed to change state in the middle of the sequence. Problem with this code now is that I should use 3 sequences for each color (fade in, hold, fade out) with middle sequence should last for period1, period2 or period3 of time and fade in always being on the begining and fade out onthe end of sequence for each color. Ill try to figure it out I found @Grumpy_Mike explaination of commutable delay where next state is triggered by some pin.

P.S. from your changes in code I need to set interval value to something when declaring it for first time because if I set it to 0 it will not hold first color almost at all for the first iteration through the loop?

I am still struggling with writing proper code for holdColor function that is supposed to keep same color and brightness for a specific amount of time before moving forward to next step. This is what I have so far:

#include <FastLED.h>

#define NUM_LEDS 20
#define DATA_PIN 5

CRGB leds[NUM_LEDS];

const unsigned long period1 = 1000;
const unsigned long period2 = 2000;
const unsigned long period3 = 3000;
unsigned long currentTime = 0;
unsigned long prevTime = 0;
unsigned long interval = 2000;
unsigned long prevColor = 0;
unsigned long currentColor = 0;
const unsigned long fadeTimer = 30;
int color = 0;
int state = 0;
int brightness = 0;
int fadeAmount = 5;


void fadeIn(int r,int g, int b) {
  Serial.println("Fade in start");
  for(int i = 0; i < NUM_LEDS; i++)
   {
    leds[i].setRGB(r,g,b);
   }
  while(currentColor - prevColor < fadeTimer && brightness<=125){
    Serial.println("Fade in while loop");
    FastLED.setBrightness(brightness);
    FastLED.show();
    brightness= brightness + fadeAmount;
  }
  prevColor = currentColor;
  Serial.println("Fade in end");
}

void fadeOut(int r,int g, int b) {
  Serial.println("Fade out start");
  for(int i = 0; i < NUM_LEDS; i++)
   {
    leds[i].setRGB(r,g,b);
   }
  while(currentColor - prevColor < fadeTimer && brightness>=5){
    Serial.println("Fade out while loop");
    FastLED.setBrightness(brightness);
    FastLED.show();
    brightness= brightness - fadeAmount;
  }
  prevColor = currentColor;
  Serial.println("Fade out end");
}

void holdColor(int r, int g, int b) {
  Serial.println("Hold color start");
  prevTime=currentTime;
  if(currentTime-prevTime >= interval) {
    Serial.println("Hold color if loop");
    for(int i = 0; i < NUM_LEDS; i++)
    {
      leds[i].setRGB(r,g,b);
    }
    FastLED.setBrightness(brightness);
    FastLED.show();
  } 
  Serial.println("Hold color end");
}



void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() {
  Serial.println("Void loop start.");
  currentTime = millis();
  currentColor= millis();
  
  switch (color) {

  case 0:
    Serial.println("Case 0 begining.");
    fadeIn(9, 151, 95);
    holdColor(9, 151, 95);
    fadeOut(9, 151, 95);
    interval=period1;
    color++;
    Serial.println("Case 0 end.");
  break;

  case 1:
  Serial.println("Case 1 begining.");
    fadeIn(0, 255, 50);
    holdColor(0, 255, 50);
    fadeOut(0, 255, 50);
    interval=period3;
    color++;Serial.println("Case 1 end.");
  break;

  case 2:
    fadeIn(95, 158, 200);
    holdColor(95, 158, 200);
    fadeOut(95, 158, 200);
    interval=period2;
    color++;
  break;

  case 3:
    fadeIn(100, 157, 240);
    holdColor(100, 157, 240);
    fadeOut(100, 157, 240);
    interval=period1;
    color++;
  break;

  case 4:
    fadeIn(65, 166, 179);
    holdColor(65, 166, 179);
    fadeOut(65, 166, 179);
    interval=period3;
    color++;
  break;

  case 5:
    fadeIn(100, 149, 237);
    holdColor(100, 149, 237);
    fadeOut(100, 149, 237);
    interval=period2;
    color++;
  break;

  case 6:
    fadeIn(0, 71, 171);
    holdColor(0, 71, 171);
    fadeOut(0, 71, 171);
    interval=period1;
    color++;
  break;

  case 7:
    fadeIn(20, 52, 164);
    holdColor(20, 52, 164);
    fadeOut(20, 52, 164);
    interval=period3;
    color++;
  break;

  case 8:
    fadeIn(128, 0, 128);
    holdColor(128, 0, 128);
    fadeOut(128, 0, 128);
    interval=period2;
    color++;
  break;

  case 9:
    fadeIn(170, 51, 106);
    holdColor(170, 51, 106);
    fadeOut(170, 51, 106);
    interval=period1;
    color++;
  break;
  
  case 10:
    fadeIn(103, 49, 71);
    holdColor(103, 49, 71);
    fadeOut(103, 49, 71);
    interval=period3;
    color++;
  break;

  case 11:
    fadeIn(159, 43, 104);
    holdColor(159, 43, 104);
    fadeOut(159, 43, 104);
    interval=period2;
    color++;
  break;

  case 12:
    fadeIn(75, 0, 130);
    holdColor(75, 0, 130);
    fadeOut(75, 0, 130);
    interval=period1;
    color++;
  break;

  case 13:
    fadeIn(93, 63, 211);
    holdColor(93, 63, 211);
    fadeOut(93, 63, 211);
    interval=period3;
    color++;
  break;

  case 14:
    fadeIn(170, 51, 106);
    holdColor(170, 51, 106);
    fadeOut(170, 51, 106);
    interval=period2;
    color++;
  break;

  case 15:
    fadeIn(251, 150, 50);
    holdColor(251, 150, 50);
    fadeOut(251, 150, 50);
    interval=period1;
    color++;
  break;

  case 16:
    fadeIn(255, 255, 0);
    holdColor(255, 255, 0);
    fadeOut(255, 255, 0);
    interval=period3;
    color++;
  break;

  case 17:
    fadeIn(255, 234, 0);
    holdColor(255, 234, 0);
    fadeOut(255, 234, 0);
    interval=period2;
    color++;
  break;

  case 18:
    fadeIn(228, 208, 10);
    holdColor(228, 208, 10);
    fadeOut(228, 208, 10);
    interval=period1;
    color++;
  break;

  case 19:
    fadeIn(223, 255, 0);
    holdColor(223, 255, 0);
    fadeOut(223, 255, 0);
    interval=period3;
    color++;
  break;

  case 20:
    color=0;
    interval = period2;
  break;
  }
}                
                        
          

I have used some prints to debug and see where code is stuck. Problem is that I do not stay in holdColor function before if loop fires, it just skips it and then gets stuck at Void Loop start.
This is what gets printed:
Void loop start.
Case 0 begining.
Fade in start.
Fade in while loop x26.
Fade in end.
Hold color start.
Hold color end.
Fade out start.
Fade out while loop x26.
Fade out end.
Case 0 end.
Void loop start -> keeps going forever.

P.S. As soon as I posted I noticed I skip case 1 for some reason o.O
Guess I have been looking at the code for too long.

If you set it to 0, it's mean that the first millis condition will match right after loop() starts, so you color will be setup at the start of program.

After fixing no case 1 my outout is like this:
Void loop start.
Case 0 begining.
Fade in start.
Fade in while loop x26.
Fade in end.
Hold color start.
Hold color end.
Fade out start.
Fade out while loop x26.
Fade out end.
Case 0 end.
Void loop start.
Case 1 begining.
Fade in start.
Fade in end.
Hold color start.
Hold color end.
Fade out start.
Fade out end.
and now it keeps going through the cases.

Oh yeah I get it now, idk I sometimes feel like I need to look at simple thing 1000 times to understand it especially after looking at code for so long.

no, your code is not good...

Note that the while loop with time condition is the same blocking as delay() itself. You shouldn't use while loops for your fade process if you want to avoid delays.
You should program each fade step as a separate state of finite state machine.

Surely not, simply a countdown element within the FSM state "fading" so that each pass through, it knows whether or not it's done. That's how I'd manage it, anyway; there's too much in common for the fading step to warrant multiple states, IMO.

Nonsense.

I think you are misunderstanding what is being said here.

The "countdown element" is an additional state variable that is used within the fadein/fadeout state. Together, the set of state variables make a state vector and are enough data for bits within loop() to make the right choices.

The OP's codes have 20 different color states each with an interval, and would like to have a fade in, hold, and fade out for each one.

Instead of a 20-color switch-case expanded to 60 cases, or a 20 color + "countdown element" vector, I'd use a state vector of [colorIndex, fadeState{fadein,hold,fadeout}, countdownElement], put the 20 different colors and their intervals in arrays, and have a 3-element switch on fadeState.

(I rediscovered graphviz for diagramming state machines)

1 Like

would equate to your "+/-Fade Increment". All I was saying.

I think I got it, pain in the ass to change colors and probably can be shorter with state array but here is what I got:

#include <FastLED.h>

#define NUM_LEDS 20
#define DATA_PIN 5


CRGB leds[NUM_LEDS];

const unsigned long period1 = 500;
const unsigned long period2 = 1000;
const unsigned long period3 = 2000;
const unsigned long fadeTime = 30;
unsigned long currentTime = 0;
unsigned long prevTime = 0;
unsigned long interval = 0;
int color = 1;
int brightness = 0;
int fadeStep = 5;




void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() {

  currentTime = millis();

  if(currentTime - prevTime >= interval){

    prevTime = currentTime;
  
  switch (color) {

  case 1:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(9, 151, 95);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness + fadeStep;
    if(brightness == 130){
      color++;
    }
  break;

  case 2:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(9, 151, 95);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period2;
    color++;
  break;

  case 3:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(9, 151, 95);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;
  
  case 4:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(0, 255, 50);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 5:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(0, 255, 50);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period1;
    color++;
  break;

  case 6:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(0, 255, 50);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;
  
  case 7:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(95, 158, 200);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 8:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(95, 158, 200);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period3;
    color++;
  break;

  case 9:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(95, 158, 200);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 10:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(100, 157, 240);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 11:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(100, 157, 240);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period2;
    color++;
  break;

  case 12:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(100, 157, 240);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 13:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(65, 166, 179);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 14:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(65, 166, 179);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period1;
    color++;
  break;

  case 15:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(65, 166, 179);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 16:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(100, 149, 237);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 17:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(100, 149, 237);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period3;
    color++;
  break;

  case 18:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(100, 149, 237);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 19:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(0, 71, 171);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 20:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(0, 71, 171);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period2;
    color++;
  break;

  case 21:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(0, 71, 171);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 22:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(20, 52, 164);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 23:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(20, 52, 164);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period1;
    color++;
  break;

  case 24:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(20, 52, 164);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 25:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(128, 0, 128);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 26:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(128, 0, 128);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period3;
    color++;
  break;

  case 27:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(128, 0, 128);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 28:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(170, 51, 106);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 29:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(170, 51, 106);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period2;
    color++;
  break;

  case 30:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(170, 51, 106);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 31:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(103, 49, 71);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 32:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(103, 49, 71);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period1;
    color++;
  break;

  case 33:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(103, 49, 71);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 34:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(159, 43, 104);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 35:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(159, 43, 104);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period3;
    color++;
  break;

  case 36:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(159, 43, 104);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 37:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(75, 0, 130);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 38:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(75, 0, 130);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period2;
    color++;
  break;

  case 39:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(75, 0, 130);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 40:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(93, 63, 211);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 41:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(93, 63, 211);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period1;
    color++;
  break;

  case 42:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(93, 63, 211);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 43:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(170, 51, 106);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 44:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(170, 51, 106);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period3;
    color++;
  break;

  case 45:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(170, 51, 106);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 46:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(251, 150, 50);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 47:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(251, 150, 50);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period2;
    color++;
  break;

  case 48:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(251, 150, 50);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 49:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(255, 255, 0);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 50:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(255, 255, 0);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period1;
    color++;
  break;

  case 51:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(255, 255, 0);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 52:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(255, 234, 0);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 53:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(255, 234, 0);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period3;
    color++;
  break;

  case 54:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(255, 234, 0);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 55:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(228, 208, 10);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 56:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(228, 208, 10);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period2;
    color++;
  break;

  case 57:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(228, 208, 10);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;

  case 58:
  for(int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(223, 255, 0);}
  FastLED.setBrightness(brightness);
  FastLED.show();
  interval = fadeTime;
  brightness = brightness + fadeStep;
  if(brightness == 130){
      color++;
  }
  break;

  case 59:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(223, 255, 0);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period1;
    color++;
  break;

  case 60:
    for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(223, 255, 0);}
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;
  
  case 61:
    color = 1;
    interval=0;
  break;
  
  }
}                
}                   

Nop.

You are failing to understand the fundamentals of what you need to do and therefore you have miles and miles of unnecessary code.

If you want to fade for a specific time and have the final value black, then you have to calculate the fade increment for that time based on how often you call the fading function. This is likely to be a number less that one and so use a float variable and an accumulation value for the current fade to apply. Then when applying it only take the integer part of the number.

But as you steadfastly refuse to implement a state machine or a commutable delay then that is what you are left with.

Oh, you should have asked about arrays before doing that pain in the ass typing.

There's FastLED.fill_solid(), and if you set up an array lper Replacing FASTLED colour name with an array index - #2 by gfvalvo

uint32_t colors[] = {FastLED::CRGB(9, 151, 95),
                     FastLED::CRGB(0, 255, 50),
                     ...
                     FastLED::CRGB(223, 255, 0)};

you can then have just three chunks:

if(color>=61) {
  color = 1;
   interval = 0;
}
switch ((color%3)+1) {

  case 1:
    FastLED.fill_solid(leds, NUM_LEDS, colors[(color-1)%3] );
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness + fadeStep;
    if(brightness == 130){
      color++;
    }
  break;

  case 2:
    FastLED.fill_solid(leds, NUM_LEDS, colors[(color-1)%3] );
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = period2;
    color++;
  break;

  case 3:
    FastLED.fill_solid(leds, NUM_LEDS, colors[(color-1)%3] );
    FastLED.setBrightness(brightness);
    FastLED.show();
    interval = fadeTime;
    brightness = brightness - fadeStep;
    if(brightness == 0){
      color++;
    }
  break;
}

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