Fade Without Delay

Hi Guys!

I am new to Arduino and am trying to make an LED pulse on and off without using a delay. It needs to be on constantly, without interfering with other parts of the code. So far, the LED works, but it flickers on and off very quickly. (But not quickly enough to be PWM). I think that that the fade parts of the code are not being affected by the if statement. Any suggestions would be great!

const int LEDfade =  10;      // the number of the LED pin

// Variables will change:
long previousMillis = 0;        // will store last time LED was updated
int i = 0;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 2500;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(LEDfade, OUTPUT);
  Serial.begin(9600); //For debugging
}

void loop() {

unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;  
      for (i = 0; i < 225; i++) { // loop from 0 to 254 (fade in)
      analogWrite(LEDfade, i); // set the LED brightness           
    }
	}
    else { 
      for (i = 225; i > 0; i--) { // loop from 255 to 1 (fade out)
      analogWrite(LEDfade, i); // set the LED brightness
    }
    }
    Serial.println(previousMillis);
}
for (i = 0; i < 225; i++) { // loop from 0 to 254 (fade in)

Guess which of those statements the compiler believes.

You're running your "for" loop every timeout very quickly, when there should be no "for" at all.
You should be running a single "analogWrite" every timeout, with the logic of the "for" written explicitly.

I think you have your method inside out.

You are using millis() to decide when to run your routine. That routine is to fade in and fade out immediately. So, you get:

/_/_/________/_

You need to keep a variable that contains the brightness, and another which contains whether you are fading up or down. Then when your interval is up you either increment or decrement the brightness depending on the direction variable, and analogWrite that brightness. Then, if the brightness is either 0 or 255 you change direction.

Thanks for the quick responses. I think that I am starting to see where I went wrong. Where should the for loops be instead?

Where should the for loops be instead?

They shouldn't be in that sketch.
But, as I said, you need the logic, albeit spread around.

You need to initialise the level, you need to test the level, and you need to increment (or decrement) the level.

I think I've got it. Now, it will fade to full brightness the first time, fade to minimum brightness, but then it wont fade back to full brightness, it will just instantly go to full. I know there is something really obvious right in front of my face but I just can't see it!

const int LEDfade =  10;      // the number of the LED pin

// Variables will change:
long previousMillis = 0;        // will store last time LED was updated
int i = 0;
int dir = 1;                    // 1 = UP, 0 = DOWN
int MAX = 200;
int MIN = 35;

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 10;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(LEDfade, OUTPUT);
  Serial.begin(9600); //For debugging
}

void loop() {

unsigned long currentMillis = millis();

  if(i == MAX) {
    dir = 0;
  }
  
  if(i == MIN) {
    dir = 1;
  }
  
  if(currentMillis - previousMillis > interval && dir == 1) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
    i = i + 2;
    analogWrite(LEDfade, i); // set the LED brightness           
    }
	
  if(currentMillis - previousMillis > interval && dir == 0) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
    i = i - 2;
    analogWrite(LEDfade, i); // set the LED brightness           
    }

}

Could it be that MIN is an odd number?
Often in code like this, "<=" or ">=" are better choices than "=="

Try setting interval to 50mS.