problem trying to do a double fade

I have just recently started using Arduino. After reading through the fade example I thought why not make what I decided to call a double fade? Basically one led would fade in while the other fades out. My Code:

int bluebrightness = 0; int yellowbrightness = 255; int fade = 5 int blue = 9; int yellow = 3; void setup(){ pinMode(blue,OUTPUT); pinMode(yellow,OUTPUT); } void loop(){ analogWrite(blue,bluebrightness); analogWrite(yellow,yellowbrightness); bluebrightness == bluebrightness + fade; yellowbrightness == yellowbrightness - fade; if (bluebrightness == 0 || bluebrightness == 255){ fade == -fade; } delay(30); }

But only the yellow led is lighting up and its not fading. Anyone have an idea what might be going on?

fade == -fade;

This jumps out at me. You're using the equality operator, not making an assignment. What you're doing is comparing fade to -fade, which is false, then you're throwing away the result.

Try using a single equals sign.

Pins 3, 9, 10, and 11 do have the same pwm clock, I don't know if this might be an issue.

I just took another look: you’re also using the double equals sign for the two brightness assignments inside loop(). Yellow is always on, because it’s value is never changing from 255, and blue is always off because it’s never changing from zero.

Also, you are checking for blue brightness to equal 0 or 255. This should work for now because your increment of 5 evenly divides 255. But if you ever changed fade to something like 4 or 10, blue brightness will never exactly equal 255. It’s good practice to use greater or equal or less or equal.

Try this:

int bluebrightness = 0;
int yellowbrightness = 255;
int fade = 5
int blue = 9;
int yellow = 3;
void setup(){
  pinMode(blue,OUTPUT);
  pinMode(yellow,OUTPUT);
}
void loop(){
  analogWrite(blue,bluebrightness);
  analogWrite(yellow,yellowbrightness);
  bluebrightness = bluebrightness + fade;
  yellowbrightness = yellowbrightness - fade;
  if (bluebrightness <= 0 || bluebrightness >= 255){
    fade = -fade;
  }
  delay(30);
}

Note that the comparisons only work because you’re using an int for blue brightness, which can get negative or greater than 255. If you had used a byte, it could wrap and the comparison could fail. For example, if fade were 10, and blue brightness was a byte with the value 250, adding 10 to it would wrap and give you a value of 4. Your comparison would never be triggered. Care is necessary. In this case you could use:

if ((bluebrightness < fade) || (bluebrightness > (255 - fade)))

mart256: Pins 3, 9, 10, and 11 do have the same pwm clock, I don't know if this might be an issue.

No, it's not an issue. In fact, in this case it's probably a benefit as it could help ensure consistent brightness.

That fixed it. Thanks a lot. I guess I just learned my lesson about comparisons and assignments Ill definitely remember that from now on.

Also keep your eyes open for the opposite: using a single equals for assignment when you mean the double equals equality check. That’s the way most people confuse them: using a single equals in an if statement, which is almost always wrong.