-1

The brightness of the LED gets higher normally but when it gets to full brightness it just stays like that - it doesn't dim. Sorry if this has already been answered I just can't seem to find anything about this exact problem online. I have also tried a few different codes from tutorials but the outcome is the same.

int led = 6;

void setup() {
  pinMode(led, OUTPUT);  
}
 
void loop() {
  for (int i  = 5; i <= 255; i+=1){
    analogWrite(led, i);
    delay(100);  
  }
  
  for (int i = 255; i >= 5; i-=1) {
    analogWrite(led, i);
    delay(100);
  }
}
2
  • tip: i++ looks neater for increments of 1
    – jsotola
    Commented Apr 27 at 18:12
  • Try the exact built-in fading code of the Arduino IDE instead.
    – tepalia
    Commented May 3 at 13:23

2 Answers 2

0

Well, provided that you hooked up the led to the right pin, it actually does.

The point is human sight is quite imperfect. When light source becomes too bright we stop seeing shades. Also, we can see better the shades of green, rather than red or blue.

So you count from 5, skipping "the darkest values", with all even delays, and, probably, with the smallest resistor value possible. Then the led's brightness soon reaches the threshold and stays too high for the most of the time. You have only few seconds out of fifty to note that it dimmed a bit and then it becomes a bright spot again.

Instead, try using uneven delays, so lower values stay longer in effect and, maybe, increase current limiting resistor value and/or limit the values passed to analogWrite (e.g. up to 200 only).

0

It worked for me but it's slow. 251 iterations (of each loop) at 0.1 sec/loop takes 25 seconds up and 25 seconds down. When I reduced the delay to 10msec, the behavior was much more obvious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.