problem fading and blinking leds simultaneously with millis

I have LED1 fading on and off with millis, and LED2 blinking with millis. There are not any delays, but when LED2 blinks ON, LED1 gets slightly dimmer, and when LED2 blinks back off, LED1 gets brighter. Is this something in my code? I suppose it could be a hardware issue, but I think it might be this code (which I borrowed from someone online). heres my code:

#define BLINK_RATE 1000 // On/off blink every 1 second
#define FADE_RATE 6000  // Fade up and down every 2 seconds

int ledPin = 5;
int pwmPin = 10;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
}

void loop()
{
  unsigned long time = millis();

  // Blink the LED
  if (time % BLINK_RATE > BLINK_RATE/2)
    digitalWrite(ledPin, HIGH);
  else
    digitalWrite(ledPin, LOW);

  // Synthesize a sawtooth wave: a value from 0 to FADE_RATE/2 and back down
  long val = labs((FADE_RATE / 2) - (time % FADE_RATE));
  val = 255 * val / (FADE_RATE / 2); // convert to range 0..255..0
  analogWrite(pwmPin, val);
}

Just tried your code on my Uno and I can't reproduce what you're seeing. How do you have these wired up? Are you sharing the resistor for both LEDs (the only way I was able to get what you're describing ...)

oh! i actually didnt use any resistors. If I add resistors, do you think that will solve it? I'll try that and see what happens. Thanks!

If I add resistors, do you think that will solve it?

Doesn’t matter. If you continue to leave off current limiting resistors you will permanently damage your board.

Hence my original question of how he had things wired up ... Code is fine, but hardware is questionable ...

The resistor was indeed the problem(it doesnt say "newbie" by my name for nothing). Sorry bout the hardware issue on the software forum. This code works. Now I'm trying to alter this code to blink sequentially through 11 LEDs while fading, instead of blinking just one LED. My attempt gave me no blinking LEDs.

#define BLINK_RATE 1000 // On/off blink every 1 second
#define FADE_RATE 5000  // Fade up and down every 2 seconds

 int x = 0;

int pwmPin = 10;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);
}

void loop()
{

  unsigned long time = millis();

  // Blink the LED

    if (time % BLINK_RATE > BLINK_RATE/2)
      digitalWrite(x, HIGH);
    else
      digitalWrite(x, LOW);
  x = x+1;
  if (x = 14)
  x = 0;
  // Synthesize a sawtooth wave: a value from 0 to FADE_RATE/2 and back down
  long val = labs((FADE_RATE / 2) - (time % FADE_RATE));
  val = 255 * val / (FADE_RATE / 2); // convert to range 0..255..0
  analogWrite(pwmPin, val);
}

thanks again!

erinbanwell:   x = x+1;   if (x = 14)   x = 0;

thanks again!

You are setting the value of x to 14. I believe you meant to compare it which means:if (x == 14)

thanks KirAsh4! Now I'm getting all the blinking LED's blinking together instead of sequentially. Should I put it in a "for" statement? If so, where would I put it?

UPDATE:
I tried using a for statement but got the same problem (all LEDs besides the fading one are blinking together instead of sequentially.
heres that code:

#define BLINK_RATE 1000 // On/off blink every 1 second
#define FADE_RATE 5000  // Fade up and down every 2 seconds

 int x = 0;

int pwmPin = 10;

void setup()
{

  pinMode(pwmPin, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);
}

void loop()
{

  unsigned long time = millis();

  // Blink the LED
 for (x = 0; x <= 13; x++){
    if (time % BLINK_RATE > BLINK_RATE/2)
      digitalWrite(x, HIGH);
    else
      digitalWrite(x, LOW);
 
  if (x == 14)
  x = 0;
 }
  // Synthesize a sawtooth wave: a value from 0 to FADE_RATE/2 and back down
  long val = labs((FADE_RATE / 2) - (time % FADE_RATE));
  val = 255 * val / (FADE_RATE / 2); // convert to range 0..255..0
  analogWrite(pwmPin, val);
}

Actually, they're not blinking together. However, because you're turning them on and 1ms second you're turning them off again, it appears as if they're all blinking together. Trust me, they're not. One simple way of testing that is to add a delay(50) before your if statement.

And by doing that, you will also discover another problem, which I will leave for you to find.

thanks. I'm betting you are referring to the delay vs. millis problem, eh? Thanks again for your help!

Nope. Has to do with your fading LED ...

what i mean is, if I add delay the fade gets all chopped up, so I need to work out a millis timer instead of delay. correct?

No, the delay nor millis() has nothing to do with it getting chopped up. Think about it for a moment. You are using pin 10 for the fade, and you are cycling from 0 to 13 to turn LEDs on/off ... 0 to 13 ... pin 10 ... pin 10 ... 0 to 13 ... Write it out if you don't quite see it. Start with x == 0. Your loop goes 0, 1, 2, 3 ... 8, 9, ?? What just happened?

I'm not trying ot be a PITA here, but I don't want to just hand you the answer either. This is how you learn (at least I do), by figuring out where my mistakes are. In your case, just step through your loop one by one, and hopefully you'll understand why your fade on pin 10 gets all chopped up. It's not that hard. If you really can't tell, I'll try to explain it.

And to further try to help you understand the problem, the delay(50) I suggested was merely to illustrate that your LEDs are in fact blinking on and off in sequence, and not all at once. So if you remove that delay you will once again get what would appear to be a smooth fade on pin 10, however that's merely an illusion. Because of your for loop, the fade gets interrupted every 13 cycles.

You should rethink how you're delaying the blinking and not interrupt the fade at the same time.

i totally get it.

Awesome. Work on it a bit, and post your findings.