Help needed in re-writing a function

Hi everyone.
I have written this code to fade an LED up in brightness using millis(), since I needed this function to be non blocking.
This is what I've written so far:

const byte pwmLED = 5;


// constants for min and max PWM

const int endValue = 127; // this is the goal value


// Global Fade Value
// but be bigger than byte and signed, for rollover
int currentValue = 40; //starting value

long rampDuration = 5000;//5 secs

// millis() timing Variable, just for fading
unsigned long previousFadeMillis = 0;


int fadeInterval = rampDuration / (endValue - currentValue);
void setup() {
  // put pwmLED into known state (off)
  analogWrite(pwmLED, currentValue);
  Serial.begin(9600);
}

void doTheFade(unsigned long thisMillis) {

  if (thisMillis - previousFadeMillis >= fadeInterval) {
    if (currentValue != endValue) {
      currentValue++;
    }
    else {
      currentValue = endValue;
    }

    analogWrite(pwmLED, currentValue);
    Serial.println(currentValue);
    previousFadeMillis = thisMillis;
  }
}


void loop() {
  // get the current time, for this time around loop
  // all millis() timer checks will use this time stamp
  unsigned long currentMillis = millis();

  doTheFade(currentMillis);
  

}

What I'd like to do now is to create a single function without having the different variables all over the place, so that I can call that function and use it whenever i need it in another sketch.

I've tried writing this function:

bool fadeLED(int initialValue, int endValue, long duration)
{
  int interval = duration / (endValue - initialValue);

  if (millis() - previousFadeMillis >= interval) {
    if (initialValue != endValue) {
      initialValue++;
    }
    else {
      initialValue = endValue;
      return 1;
    }

    analogWrite(pwmLED, currentValue);
    Serial.println(currentValue);
    
    previousFadeMillis = millis();
  }
}

But the problem is that since initialValue gets increased, the interval between the updates of the fade value becomes larger as time goes on, which is not what I want (and also something that doesn't happen in the code posted above).
I'd be glad if someone could help me writing the function so that it works properly.
Thanks

When using your second function where does currentValue get initialised ?

Whoops, I meant to write initialValue. Still, the problem persists

bool fadeLED(int initialValue, int endValue, long duration)
{
  int interval = duration / (endValue - initialValue);

  if (millis() - previousFadeMillis >= interval) {
    if (initialValue != endValue) {
      initialValue++;
    }
    else {
      initialValue = endValue;
      return 1;
    }

    analogWrite(pwmLED, initialValue);
    Serial.println(initialValue);
   
    previousFadeMillis = millis();
  }
}
{
      initialValue = endValue;
      return 1;
    }

What’s the point of the assignment?

I wanted to stabilize the value after the "ramp", but it's not, infact, necessary because all I care about is to return 1 once the ramp is over (and initialValue will be equal to end value after the first if)

Anyone that could help?

Anyone that could post your updated code?

What could be the problem with letting much less setting initialValue = endValue?

int interval = duration / (endValue - initialValue);

also that fadeLed function only has 1 return.

ALL of your time variables should be the same type or cast to the same.

int currentValue = 40; //starting value

long rampDuration = 5000;//5 secs

// millis() timing Variable, just for fading
unsigned long previousFadeMillis = 0;

You do a lot of mixed-types math. I wonder if any gets unintended results?