I have a code that fades an LED in and out. Defined are variables to adjust the min/max brightness and fade Increment. So far everything works. I am trying to add two new variables to define a duration to hold at max brightness and to hold again at min brightness... of course without using Delay().
The code is integrated in a much larger script, so I am posting only the most relevant parts:
Definitions:
// define directions for LED fade
#define UP 0
#define DOWN 1
Constants:
const byte pwmLED = 11; // Requires a PWM pin.
const int minPWM = 0; // Limit for min PWM
const int maxPWM = 255; // Limit for max PWM
byte fadeIncrement = 5; // Smoothness of fade transition
const int minPWMduration = 5000; //Once the min value is reached, remain for this duration
const int maxPWMduration = 1000; //Once the max value is reached, remain for this duration
byte fadeDirection = UP; // Variable for Fade Direction
int fadeValue = 0; // Global Fade Value - bigger than byte and signed, for rollover
Statement in void setup:
analogWrite(pwmLED, fadeValue); // put pwmLED into known state (off)
Statement in void loop:
FadeLED(currentMillis);
The function that is called out:
void FadeLED(unsigned long currentMillis) {
// is it time to update yet? if not, nothing happens
if (currentMillis- previousFadeMillis >= fadeInterval) {
// Going UP
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
//***************************************************************
// AnalogWrite does not go to full brightness *
//***************************************************************
if (maxPWM == 255) {digitalWrite(pwmLED, HIGH); // Turn LED at full bright if a PWM value of 255 is needed
if(millis() <= currentMillis+ maxPWMduration){ //wait the ON duration
fadeValue = maxPWM; // At max, limit and change direction
fadeDirection = DOWN;
}
}
}
} else {
// Going DOWN
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
//***************************************************************
// AnalogWrite minimums out at a brightness of about 3 (NOT 0) *
//***************************************************************
if (minPWM == 0) {digitalWrite(pwmLED, LOW); // Turn LED off if a PWM value of 0 is needed
if(millis() <= currentMillis + minPWMduration){ //wait the OFF Duration
fadeValue = minPWM; // At min, limit and change direction
fadeDirection = UP;
}
}
}
}
// Only need to update when it changes
// analogWrite(pwmLED, fadeValue); // executed in Switch LEDs Function above
previousFadeMillis = currentMillis; // reset millis for the next iteration (fade timer only)
}
}
The min and max durations (minPWMduration & maxPWMduration) are nested in an IF, everything compiles successfully, but there is no delay at either brightness level.
I tested this with a WHILE statement just to see if the idea in general was sound, and it worked fine, so I am guessing I simply have set up my IF incorrectly, but I do not see what I am doing wrong. I would be grateful for any hints as to how to correct this.