This is admittedly a cross-post from LED fade malfunction (random flash) 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, Arduino - fading LED problem.
analogWrite(led, 255);
as it's quite useless, given that two lines below it, it is set back to 0. Can't see anything really wrong with the code (other that a bit of a weird order).delay(6)
todelay(20)
- does the flash appear? You've got the second code rolling round three times faster than the first code (ignoring the 1-second delay, of course), so the random flash is probably undetectable. As for why there's a flash at all... no idea.