2 LEDs Fading over time (again but different instance)

Hi guys, I really need some help here. Basically what I'm trying to do is fade 2 LED's at the same rate, with a push of a button. One going down and the other going up at the same time, over 90 seconds. It also pauses for 10 seconds before it starts the fade. The following sketch works. It's probably just a very ass backwards way of doing it using arrays and nothing like the blink without delay sketch.

#define FADE_TIME 32000
#define NUM_OUTPUTS 2

struct stOutput
{
  int nPin;
  unsigned long nFadeTime;
  unsigned long nCurrentTime;
  unsigned  long nTimerStart;
  int nStart;
  int nEnd;
};

stOutput arrOutputs[NUM_OUTPUTS] = {{10, FADE_TIME, 0, 0, 255, 0}, {11, FADE_TIME, 0, 0, 0, 255} };

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
 
  //Serial.println(map(40000, 90000, 0, 255, 0));
}

void loop() {
  // put your main code here, to run repeatedly: 
  FadeOutputs();
  
  if( digitalRead(2) == HIGH )
  {
    ResetOutputs();
  }
}

void Start()
{
  bool bStarted = false;
  
  for (int i=0; i<NUM_OUTPUTS; ++i)
  {
      if( arrOutputs[i].nTimerStart != 0 )
      {
        bStarted = true;
        break;
      }
  }
  
  if( !bStarted )
  {
    for (int i=0; i<NUM_OUTPUTS; ++i)
    {
        analogWrite(arrOutputs[i].nPin, arrOutputs[i].nStart);
    }
    
    delay(10000);
  }
}

void ResetOutputs()
{
  Start();
  
  for (int i=0; i<NUM_OUTPUTS; ++i)
  {
    if( arrOutputs[i].nTimerStart == 0 )
    {
      arrOutputs[i].nCurrentTime = (arrOutputs[i].nStart < arrOutputs[i].nEnd) ? 0 : arrOutputs[i].nFadeTime; 
      analogWrite(arrOutputs[i].nPin, arrOutputs[i].nStart);
      arrOutputs[i].nTimerStart = millis();
    }
  }
}

void FadeOutputs()
{
  for (int i=0; i<NUM_OUTPUTS; ++i)
  {
    if (arrOutputs[i].nTimerStart != 0)
    {
      unsigned long nTime = millis()-arrOutputs[i].nTimerStart;
      
      if(nTime >= 5)
      {
        if( arrOutputs[i].nStart < arrOutputs[i].nEnd )
        {
          if((long)arrOutputs[i].nCurrentTime + nTime >= FADE_TIME)
          {
            arrOutputs[i].nTimerStart = 0;
            arrOutputs[i].nCurrentTime = 0;
          }
          else
            arrOutputs[i].nCurrentTime += nTime;
        }
        else
        {
          if((long)(arrOutputs[i].nCurrentTime - nTime) <= 0)
          {
            arrOutputs[i].nTimerStart = 0;
            arrOutputs[i].nCurrentTime = 0;
          }
          else
            arrOutputs[i].nCurrentTime -= nTime;
        }
        
        int nStartTime = (arrOutputs[i].nStart < arrOutputs[i].nEnd) ? 0 : arrOutputs[i].nFadeTime;
        int nEndTime = (arrOutputs[i].nStart < arrOutputs[i].nEnd) ? arrOutputs[i].nFadeTime : 0;
        int nValue = map(arrOutputs[i].nCurrentTime, nStartTime, nEndTime, arrOutputs[i].nStart, arrOutputs[i].nEnd);
        Serial.println(nValue);
        analogWrite(arrOutputs[i].nPin, nValue);
        
        if( arrOutputs[i].nTimerStart != 0 )
          arrOutputs[i].nTimerStart = millis();
      }
      
    }
  }
}

The ONLY problem I have left is that it only works when my fade time is 30 seconds (not 90) if I increase it anymore than 32 seconds or so. My PWM values start doing weird crazy things like starting at 800 and not obeying the code as it was with 30 seconds and below. Its as if the map function just breaks when it has to use numbers above a certain value even though its using a unsigned long.

My apologies for the long code :frowning: I have no way to shorten to explain what I mean.

I Hope someone can help. Thanks!

anymore than 32 seconds or so

That is a clue that an int is rolling over.

What do you mean an int is rolling over?

It usually suggests you've got 16 bit signed arithmetic where you should have 32 bit.

Razzik:
What do you mean an int is rolling over?

It wasn't a typo - it really was int is rolling over. It just means that the count gets to 2^15 - 1 (for a signed int) or 2^16 -1 = 65535 (for an unsigned int) and then it goes back to 0. If that is what is happening you need to define the variable as long or unsigned long. An unsigned long won't roll over until 2^32 - 1.

Simplest thing might be to define fadeTime as an unsigned long variable rather than use #define.

...R