Get a LED to fade out I want to light an LED in a sketch and then have it fade out.
I tried to use analogWrite and have it decrease from a value of 255 to 0 using a for() loop, but I can't seem to get it to work.
Using digitalWrite I can turn it on and off but cant get it to fade out.
Can anyone help please.

const int ledPin1 = 2;
const int ledPin2 = 3;
const int ledPin3 = 9;
int j = 0;

void setup() {
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
}

void loop() {
    j = 255;
    delay(1000);
    if (j &gt;= 5);
    {
        analogWrite(ledPin3,j);
        delay(10);
        j = j - 5;
    }
    delay(1000);
}
Not all output pins are able to perform PWM ("AnalogWrite").


  On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.


https://www.arduino.cc/en/Reference/AnalogWrite

Note that the PWM-enabled ports are marked by a "~" on the board.

If you try analogWrite, e.g., on port 4, you'll get your observed switch behaviour.