Fading two LED's

Hello all

i am trying to fade two Led's together in the fading project, but when i try and make them both fade, only one fades and the other blinks.

here is what i did so far :

/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int led2 = 8; // the pin that the LED is attached to
int brightness2 = 0; // how bright the LED2 is
int fadeAmount2 = 5; // how many points to fade the LED2 by

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
analogWrite(led2, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
brightness2 = brightness2 + fadeAmount2;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255)
if(brightness2 == 0 || brightness2 == 255) {
fadeAmount = -fadeAmount ;
fadeAmount2 = -fadeAmount2 ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);

}


and this is the original:

/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}


Please can you help me

Are you using Uno? PWM pin only 3;5;6;9;10;11 , pin 8 was not PWM pin so it can't fade.

"analogWrite" only works with specific pins: analogWrite() - Arduino Reference

BillHo:
Are you using Uno? PWM pin only 3;5;6;9;10;11 , pin 8 was not PWM pin so it can't fade.

Yes Mine is an Uno, and when i change my pin from 8 to 10 it actually started working, thanks allot for helping me man

MrMark:
"analogWrite" only works with specific pins: analogWrite() - Arduino Reference

Thank you very much for your help