Trying to remove delay in function of color crossfade

I am trying to modify the code written by Clay Shirky that cross fades from one RGB LED color to the next to remove the delays so that I can add this code to my DMX controller project which will hopefully accept input from a MIDI foot controller, IR remote or audio sensor at anytime.

He has 2 delays in use. I was able to remove the one in the main loop by using millis() but I am at a loss on how to remove the delay inside his crossFade function.

My thinking was to create currentMillis2 & previousMillis2 variables and insert a similar if statement like I used in the main loop. My guess was I was supposed to put them in the for loop of the crossFade function but it causes the LED to only do the first iteration of the fade.

Any help would be appreciated as I am a crap programmer.

Working code with one delay left is attached (as I exceeded the 9000 character limit):

Here is my lame attempt at the function:

void crossFade(int color[3]) {

  unsigned long previousMillis2 = millis() - fadeTime;        // will store last time LED was faded

  // Read array variables into R G & B
  int R = color[0];
  int G = color[1];
  int B = color[2];

  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G); 
  int stepB = calculateStep(prevB, B);

  for (int i = 0; i <= 1020; i++) {

  unsigned long currentMillis2 = millis();

  if (currentMillis2 - previousMillis2 >= fadeTime) {

    previousMillis2 = currentMillis2;
    
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);

    analogWrite(redPin, redVal);   // Write current values to LED pins
    analogWrite(grnPin, grnVal);      
    analogWrite(bluPin, bluVal); 

  
    //delay(fadeTime); // Pause for 'fadeTime' milliseconds before resuming the loop

    if (DEBUG) { // If we want serial output, print it at the 
      if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
        Serial.print("Loop/RGB: #");
        Serial.print(i);
        Serial.print(" | ");
        Serial.print(redVal);
        Serial.print(" / ");
        Serial.print(grnVal);
        Serial.print(" / ");  
        Serial.println(bluVal); 
      } 
      DEBUG += 1;
    }
   }
  }
  // Update current values for next loop
  prevR = redVal; 
  prevG = grnVal; 
  prevB = bluVal;
  //delay(sceneTime); // Pause for optional 'sceneTime' milliseconds before resuming the loop
}

Color_CrossFade_No_Delay.ino (8.39 KB)

Yes, but any time you have numbers in your variable names then you are doing the wrong thing or you need an array.

It looks like this is a function. That is good. That means you can have a variable local to the function which can even have the same name as a similar variable elsewhere. Functions controlling motors and lights and whatever could all use different local variables called previousMillis. But that's also a source of errors if you forget which is which. Why not give this variable a unique name? Call it PreviousColorFadeMills?

Within the function, local variables will lose their values when the function ends. You should declare this variable as static so that it remembers its value in between function calls.