nested loop

Hi.

I was expecting that the following code would turn out in a 3 seconds delay with the led showing brightness 0, then a quick fade in and fade out, and then again 3 seconds delay with brightness 0.

But the delay (3000); is actually having an influence in the fade´s rate. why is that?

How do I achieve my goal?

thanks in advance.-

int ledcontrol = 9;
int brightness = 0;
int fadeAmount = 1;


void setup() {
  pinMode(ledcontrol, OUTPUT);
}


void loop() {
  delay (3000);
  fadeLed ();
}



void fadeLed() {

  analogWrite(ledcontrol, brightness);

  brightness = brightness + fadeAmount;

  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(10);
}

A delay() stops all activity.

To see how to fade with no use of delay() have a look here as an example.

Thank you very much.

I still dont understand where can I write that in between fades, I need 3 seconds of analogWrite(pwmLED, 0);

If is add the following to the loop, I still have the same problem:

analogWrite(pwmLED, 0);  
delay (3000);

Here is the code from the link without the comments:

const byte pwmLED = 9;
 
#define UP 0
#define DOWN 1

byte fadeDirection = UP;
byte fadeIncrement = 1;
 
const int minPWM = 0;
const int maxPWM = 255;
 
int fadeValue = 0;
int fadeInterval = 10;
 
unsigned long previousFadeMillis;
 
 
void setup() {
  analogWrite(pwmLED, fadeValue); 
}
 
 
void loop() {
  unsigned long currentMillis = millis();
    
  doTheFade(currentMillis);
 
}


void doTheFade(unsigned long thisMillis) {
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement;  
      if (fadeValue >= maxPWM) {
        fadeValue = maxPWM;
        fadeDirection = DOWN;
      }
    } else {
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM) {
        fadeValue = minPWM;
        fadeDirection = UP;
      }
    }
    analogWrite(pwmLED, fadeValue);  
 
    previousFadeMillis = thisMillis;
  }
}

I haven't tried this, so it may need some work, but give it a try:

#define FADEDELAY   30  // Make the change 3x slower. Change as needed

int ledcontrol = 9;
int brightness = 0;
int fadeAmount = 1;


void fadeLed() 
{
  int i;
  
  for (i = 0; i < 255; i++) {
    analogWrite(ledcontrol, i);   // Since the fade amount is 1, just use i
    delay(FADEDELAY);             // pause for this long between levels...
  }

}

void setup() {
  pinMode(ledcontrol, OUTPUT);
}


void loop() {
  delay (3000);       // This makes it wait 3 seconds between "fades". Maybe decrease a bit?
  fadeLed ();
}

If you want the fade change to be something other than 1, you'll need to add it back into the code.

thank you. Your code works, except that is missing the fade out, but then I really dont understand why the code I posted in #1 doesnt work. It is the same principle isnt it?+

also, after the first loop, the delay is done with the brightness value at 255, and not at 0 where I would need it.

No, it's not the same principle - your fade routine makes one change to the fade level of the led, then returns to loop where it will hit the delay(3000) again.

Econjack's fadeled routine does the entire fade before returning so you only get one delay per fade cycle.

You just need to add a second for loop to do the fade down. A lot like the first loop but starting at 255 and going down to 0.

Steve

ok, so I am trying to add the fade out to Econjack's routine, and I did the following, but I have a very strange behaviour: during the delay, the led is not completely off, but dimply lit. Am I doing a mistake?

#define FADEDELAY   10

int ledcontrol = 9;
int brightness = 0;
int fadeAmount = 1;


void setup() {
  pinMode(ledcontrol, OUTPUT);
}


void loop() {
  delay (3000);
  fadeLed ();
}


void fadeLed() {
  int i;

  for (i = 0; i < 255; i++) {
    analogWrite(ledcontrol, i);
    delay(FADEDELAY);
  }
  for (i = 255; i > 0; i--) {
    analogWrite(ledcontrol, i);
    delay(FADEDELAY);
  }
}

You just have the big delay in the wrong place.

const byte ledcontrolPin = 9;
int brightness = 0;
int fadeAmount = 1;

void setup()
{
  analogWrite(ledcontrolPin, 0);
  delay (3000);
}

void loop()
{
  fadeLed ();
}

void fadeLed()
{
  analogWrite(ledcontrolPin, brightness);

  brightness = brightness + fadeAmount;

  if (brightness < 0 || brightness > 255)
  {
    fadeAmount = -fadeAmount;
    brightness = constrain(brightness, 0, 255);
    analogWrite(ledcontrolPin, 0);
    delay (3000);  // Delay between fades, with LED off
  }
  
  delay(10);  // Delay between steps of the fade
}

thanks!

why using

#define fadeDelay 100

instead of

int fadeDelay = 100;

?

camilozk:
why using

#define fadeDelay 100

instead of

int fadeDelay = 100;

?

Don't use define, when you can use const int (note the 'const'). Macros don't offer type safety and don't obey scopes.