This is admittedly a cross-post from [http://forum.arduino.cc/index.php?topic=360468.0][1] but I can't get an answer on the Arduino forum.

I was mucking around with some very basic code and I noticed that when repeatedly holding an LED at 0 brightness for 1 second and then fading in to full brightness, a small flash would occasionally happen at the beginning of each fade (seemingly random). 

 int led = 11;
 int brightness = 0;
 
 void setup() 
 { 
 pinMode(led, OUTPUT);
 } 
 
 
 void loop() 
 { 
 if(brightness >= 256) //checks if brightness has passed 255, resets to 0
 {
 analogWrite(led, 255);
 brightness = 0; 
 }
 analogWrite(led, brightness);
 if(brightness == 0)
 {
 delay(1000); //LED off for 1 second
 }
 
 brightness+=1; //increment brightness
 delay(20);
 }

So, the thing that has me completely perplexed is that I can use a different piece of code (below) and the flash goes away!

 int i = 0;
 int led = 11;
 
 void setup()
 {
 pinMode(led,OUTPUT);
 }
 
 void loop()
 {
 analogWrite(led, i);
 delay(6);
 if(i%256 == 0)
 {
 i = 0 ;
 delay(1000);
 }
 i++;
 }


Has anyone got any clue as to why this would happen? Both programs have basically the same code, except for that i is reset to 0 in the first program whereas in the second, i keeps incrementing past 255 so that analogWrite 'overflows.' I think it must be a firmware, (or maybe a software?????) problem.

There is a video on youtube of it happening here [https://www.youtube.com/watch?v=mbBJWjYScng][2]


 [1]: http://forum.arduino.cc/index.php?topic=360468.0
 [2]: https://www.youtube.com/watch?v=mbBJWjYScng