fade program only works for number 5

the "fade effect only works both ways with digit 5, any other number, including multiples of 5 cause only a one way fade effect?
Thanks

/

int led =9;
int brightness = 0; //how bright the LED gets.
int fadeAmount = 5; //how many points to fade the LED by.
void setup() {
// setting up the the LED.
pinMode(led, OUTPUT);
}

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

// change the brightness of the LED 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);
}
/

  if (brightness == 0 || brightness == 255)

There is your problem.

If you set fadeAmount to any value other than 5 (or 1) will brightness ever equal 0 or 255 ?
Try printing it and watch its value

Very grateful, thanks

UKHeliBob:

  if (brightness == 0 || brightness == 255)

There is your problem.

If you set fadeAmount to any value other than 5 (or 1) will brightness ever equal 0 or 255 ?

Um... 17? :slight_smile:

Montmorency:
Um... 17? :slight_smile:

This reminds me of a Monty Python sketch

Nobody expects the Spanish Inquisition!

Our chief weapon is surprise, surprise and fear, fear and surprise. Our two weapons are fear and surprise, and ruthless efficiency. Our three weapons are fear and surprise and ruthless efficiency and an almost fanatical dedication to the pope.

And nice red uniforms!