3

I am trying to do something very simple. I want the LED to fade from full brightness to off. This works... However, the LED turns back on to full brightness and fades again, in a loop. I am not sure why. I think I am missing something obvious. Once the legThreeBrightness decrements to 0, nothing should happen...

int legThreeBrightness = 255;

void setup() {

pinMode(3, OUTPUT);

}

void loop() {

if (legThreeBrightness = 255){
    do {
          analogWrite(3, legThreeBrightness);
          delay(30);
          legThreeBrightness = legThreeBrightness -5;
       } while (legThreeBrightness >= 0);
}

}

Keltari
  • 163
  • 6

1 Answers1

9

This line:

if (legThreeBrightness = 255){

assigns the value 255 to legThreeBrightness. That is non-zero so it enters the block and runs the fade now that the variable has been set to 255.

If you just want to compare the value to 255 then use:

if (legThreeBrightness == 255){

and that way when the loop function repeats it isn't there setting the value back to 255. This way it will stay at 0.

Delta_G
  • 3,391
  • 2
  • 13
  • 24