LED fade question

I am experiencing a strange thing with my LEDs. I will fade the led’s from 0 to 200 with pwm. Then digital write LOW to the LEDs. A minute or two later when a fade from 0-200 begins again. the first instant the LEDs turn on, they flash brightly before properly fading up.

I used the slow mo function of my iPhone to confirm that this is happening and that it isn’t just my eyes tricking me.

I created a small bit of code similar to my larger sketch to try and trouble shoot what is happening. I can replicate the bright flash with this code but can’t find any reason in my code that it does so. Is this simply the LED holding some charge from being near full brightness and turned off? If I fade the LED’s down before turning them off will it prevent this from happening?

int led = 11; 
unsigned long fadeTimer = 0;
unsigned long serialTimer =0;
int minusloopTrack = 1;
int fadeBegin = 0;
int fadeCount = 0;
int brightness = 0;
int fadeAmount = 1;


void setup() {
 // put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(9600);
}

void loop() {
 // put your main code here, to run repeatedly:
unsigned long currentMillis = millis();

if (analogRead (A5) >=500)
{
 minusloopTrack = 2;
 
}
else
{
 fadeBegin = 0;
 fadeCount = 0;
 brightness = 0;
 minusloopTrack = 1;
 digitalWrite (led,LOW);
}
if (minusloopTrack >=2)
{
 fadeBegin = 1;
}


if ((fadeBegin == 1) && (currentMillis - fadeTimer >= (120)) && (fadeCount <254))
{

 Serial.println ("normal fade loop");
 Serial.println (brightness);
 analogWrite(led, brightness);
 brightness = brightness + fadeAmount;
 fadeTimer = currentMillis;
 fadeCount++;
}


if (fadeCount >= 254)
{
 Serial.println ("Fade count over 254");
 Serial.println (brightness);
 fadeBegin = 0;
 fadeCount = 0;
 brightness = 0;
 analogWrite (led, 1);
 digitalWrite (led, LOW);

}




}

Please go and read the form rules, then edit your post above so it does not break the rules.

Could your flash issue be caused by using both digitalWrite() and analogWrite() on the same pin? Why are you doing that?

Or if you don't want fuzz, see my signature ;)

PaulRB: Please go and read the form rules, then edit your post above so it does not break the rules.

Could your flash issue be caused by using both digitalWrite() and analogWrite() on the same pin? Why are you doing that?

sorry about that, I think i fixed it.

I didnt think it mattered whether I used digital or analog writes for full on and full off of the LED. I modified the code to just use analogWrite and it exhibits the same behavior.

I modified the code to just use analogWrite and it exhibits the same behavior.

I still see both in your code. If you update your code, re-post it. Also please do Tools-->Auto Format before you post it.

PaulRB:
I still see both in your code. If you update your code, re-post it. Also please do Tools–>Auto Format before you post it.

int led = 11;
unsigned long fadeTimer = 0;
unsigned long serialTimer = 0;
int minusloopTrack = 1;
int fadeBegin = 0;
int fadeCount = 0;
int brightness = 0;
int fadeAmount = 1;


void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentMillis = millis();

  if (analogRead (A5) >= 500)
  {
    minusloopTrack = 2;

  }
  else
  {
    fadeBegin = 0;
    fadeCount = 0;
    brightness = 0;
    minusloopTrack = 1;
    analogWrite (led, 0);
  }
  if (minusloopTrack >= 2)
  {
    fadeBegin = 1;
  }


  if ((fadeBegin == 1) && (currentMillis - fadeTimer >= (120)) && (fadeCount < 254))
  {

    Serial.println ("normal fade loop");
    Serial.println (brightness);
    analogWrite(led, brightness);
    brightness = brightness + fadeAmount;
    fadeTimer = currentMillis;
    fadeCount++;
  }


  if (fadeCount >= 254)
  {
    Serial.println ("Fade count over 254");
    Serial.println (brightness);
    fadeBegin = 0;
    fadeCount = 0;
    brightness = 0;
    analogWrite (led, 1);
    analogWrite (led, 0);

  }




}

The code above never calls analogWrite() with values other than 0 or 1. Neither does your original code, now I look at it again. Is this code supposed to perform the fade or just reproduce the flash?

PaulRB: The code above never calls analogWrite() with values other than 0 or 1. Neither does your original code, now I look at it again. Is this code supposed to perform the fade or just reproduce the flash?

 analogWrite(led, brightness);
 brightness = brightness + fadeAmount;
 fadeTimer = currentMillis;
 fadeCount++;

This bit right here performs the fade. The LED fades correctly (minus the initial occasional flash)in the above sketch.

Sorry! Don't know why I didn't see that!

OK, I'm pretty sure it is a software problem, but I have not yet spotted were the fault is. So let's eliminate hardware problems. Load the standard "fade" sketch from the examples menu. Do you get smooth fading with no flashes?