I have a question on a sketch'Arduino

have a question
i write a new sketch about fading which
int LED = 5;
int brightness = 0;
int fadeAmount = 5;
int levelOfBrightness = 0;

void setup() {
pinMode(LED,OUTPUT);
}

void loop() {
levelOfBrightness = brightness + fadeAmount;
analogWrite (LED,levelOfBrightness);

if (levelOfBrightness == 0 || levelOfBrightness == 255) {
fadeAmount = -fadeAmount;
  
}

delay (30);
}

there is no problem in the sketch put it didn’t work out, why ?
plz help me :wink:

Hello, is this a support request for a Wayne and Layne product?

What do you mean by “it didn’t work out”? Providing details about what you expect, and what you actually observe, really help make it easier for other people to help.

Since you didn’t say what you wanted to do, I can guess from your code that you want the LED to fade between off and on. The problem is that each time through the loop() brightness is exactly the same, it is never updated. Try this code instead. I have removed the levelOfBrightness variable and instead just use the brightness variable. Each time through loop we add fadeAmount to brightness, and use that with analogWrite(). If the brightness value is at 0 or 255, we negate the fadeAmount variable. Hope that makes sense.

int LED = 5;
int brightness = 0;
int fadeAmount = 5;

void setup() {
    pinMode(LED, OUTPUT);
}

void loop() {
    brightness = brightness + fadeAmount;
    analogWrite(LED, brightness);
    if ((brightness == 0) || (brightness == 255)) {
        fadeAmount = -fadeAmount;
    }

    delay(30);
}