Running 2 pwm pins at the same time problem

I want led 1 to fade on, then off, then 2 to start when 1 is almost done. Then when 2 is fading off, I'll be doing the same thing with led 3 and then 4 as well.

It'll be run alone in a loop.

I'll put something together tomorrow. The way this will work, though, is that there are 5 intervals to consider.

The first is while the first LED fades on.

The second is while the first LED fades down and the second LED is not on.

The third interval is while LED 1 finishes fading down and the 2nd LED fades up.

The fourth interval is while LED 2 fininshes fading up.

The fifth interval is while LED 2 fades down.

If you're going to fade another LED up while LED 2 finishes fading down, the fifth interval gets shortened by the overlap. The fifth interval then looks like the third interval, except that different LEDs are involved.

In fact, you could really break the first interval in two. The first interval would look like the third interval, except there is no LED to fade down.

If you put the pins in an array, you could create subroutines for each interval, and pass the subroutines the indexes into the pin array.

I'm just not going to get involved in all this off topic stuff.

This code will fade 2 LEDs at the same time:

/*
 Turns on an LED on for one second, then off for one second, repeatedly.
 */

int ledGrn =  5;
int ledYlw =  6;
int ledRed =  9;
int ledBlu = 10;

// The setup() method runs once, when the sketch starts

void setup()
{                
  // initialize the digital pin as an output:
  pinMode(ledGrn, OUTPUT);
  pinMode(ledYlw, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledBlu, OUTPUT);
  Serial.begin(9600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void fade(int start, int end, int incr, int pause, int pin1, int pin2)
{
//  Serial.print("\nFade\n");
  if(incr > 0)
  {
    for(int i=start; i<end; i+=incr)
    {
      if(pin1 > 0)
      {
        analogWrite(pin1, i);
//        Serial.print("Set pin ");
//        Serial.print(pin1, DEC);
//        Serial.print(" to ");
//        Serial.println(i);
      }
      if(pin2 > 0)
      {
        analogWrite(pin2, end-i);
//        Serial.print("  Set pin ");
//        Serial.print(pin2, DEC);
//        Serial.print(" to ");
//        Serial.println(start-i);
      }
  
      delay(pause);
    }
  }
  else
  {
    for(int i=start; i>end; i+=incr)
    {
      if(pin1 > 0)
      {
        analogWrite(pin1, i);
//        Serial.print("Set pin ");
//        Serial.print(pin1, DEC);
//        Serial.print(" to ");
//        Serial.println(i);
      }
      if(pin2 > 0)
      {
        analogWrite(pin2, end-i);
//        Serial.print("  Set pin ");
//        Serial.print(pin2, DEC);
//        Serial.print(" to ");
//        Serial.println(start-i);
      }
  
      delay(pause);
    }
  }
}

void loop()                     
{
  int pause = 10;
  int incr = 1;
  fade(0,   255, +incr, pause, ledGrn, -1);
  fade(255,  80, -incr, pause, ledGrn, -1);
  fade(80,    0, -incr, pause, ledGrn, ledYlw);
  fade(80,  255, +incr, pause, ledYlw, -1);
  fade(255,  80, -incr, pause, ledYlw, -1);
  fade(30,    0, -incr, pause, ledYlw, ledGrn);

//  fade(0, 255, +incr, pause, ledGrn, -1);
//  fade(255, 0, -incr, pause, ledGrn, -1);
//  fade(0, 255, +incr, pause, ledYlw, -1);
//  fade(255, 0, -incr, pause, ledYlw, -1);
//  fade(0, 255, +incr, pause, ledRed, -1);
//  fade(255, 0, -incr, pause, ledRed, -1);
//  fade(0, 255, +incr, pause, ledBlu, -1);
//  fade(255, 0, -incr, pause, ledBlu, -1);
}

When I run it, I have an issue with two lights on at once. The light level seems to jump on both LEDs when the second one starts coming on, and then drop again when the first LED fades completely out. With the Serial.print statements not commented out, the correct values are written to the serial monitor, so I think that the problem is with my LEDs and resistors. Perhaps I need to get out my DMM…

Ignore the stupid comment at the top. It is wrong.

It's not just you having the problem. Same thing happens with me.

Hmm. I'll see if I can get this working again. You gave me an idea with the interval stuff.