Theoretical Question about millis or for statements

Hello everyone. I'm just wondering if this can be done and if yes, where can I find an example. I've tried a millis form and a for loop with no luck.

I can fade an LED in and out using either method. What I am trying to achieve is this;

Led one fades in, when it reaches max pwm (brightness), a second LED fades in while the first LED fades out.

Any help appreciated.

Quite possible using for loops if you don't mind blocking code

Quite possible using millis() timing if you want non blocking code. Let loop() do what it is best at

What have you tried ?

Sounds simple enough.

Please show your best effort and describe what it does and what you would like it to do instead.

A for loop is really just a while loop with initialization. It's not that hard to translate into a state machine.

I know it sounds simple to you all but I'm sitting here looking at this:

for (int i = 0; i < 255; i++) { //if i is less than 255 then increase i with 1
analogWrite(pwmLED, i); //write the i value
delay(20); //wait 5 ms then loop again
}
for (int i = 255; i > 0; i--) { //descrease i with 1
analogWrite(pwmLED, i);
delay(5);
analogWrite(pwmLED, LOW);
}


and this, with no idea where to start

const byte pwmLED = 5;
 
#define UP 0                                 
#define DOWN 1
 
const int minPWM = 0;                    // constants for min and max PWM
const int maxPWM = 255;
 
byte fadeDirection = UP;                  // Fade Direction

int fadeValue = 0;           
 
byte fadeIncrement = 5;                     // How smooth to fade?
  
unsigned long previousFadeMillis;        // timing Variable, 
unsigned long previousHighMillis;        //How long to stay high          
int fadeInterval = 45;                           // increment?
int highInterval = 2000;         
void setup() {
  
  analogWrite(pwmLED, fadeValue);                        
}
 void doTheFade(unsigned long thisMillis) {
  
    if (thisMillis - previousFadeMillis >= fadeInterval) {    
    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement;  
      if (fadeValue >= maxPWM) {        
        fadeValue = maxPWM;                                       // At max, limit and change direction

        if (fadeValue = maxPWM)  {
         digitalWrite(pwmLED, HIGH);        }
        if(thisMillis - previousHighMillis <=   highInterval)  {        }
        fadeDirection = DOWN;
      }
    } else {
      //if we aren't going up, we're going down
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM) {
        // At min, limit and change direction
        fadeValue = minPWM;
        digitalWrite(pwmLED, LOW);
        
        fadeDirection = UP;
      }
    }   
    analogWrite(pwmLED, fadeValue);      // update when it changes
 
   
                                                                 // reset
  }
}
 
void loop() {
  
  unsigned long currentMillis = millis();
   
  doTheFade(currentMillis);
 
}

Here is how you implement a for loop using a state machine:

for(i=3; i<6; i++) {<somecode>}

decomposes as the sequence

i=3;
while (i<6) {
<somecode>
i++;
}

For the state machine you would do something like

if (state == INITIALIZE) {
 i=3;
 state = RUN;
}
else if (state == RUN and i<6) {
 <somecode>
 i++;
}
else {
 state = IDLE;
}

To activate it, you just assert:

state = INITIALIZE;

Thanks aarg :slight_smile:

For me, state machines are almost always easier to read, understand and maintain using switch/case

switch (state)
  {
  INITIALIZE:
    i = 3;
    state = RUN;
    break;
  RUN:
    if (i < 6)
    {
      <somecode>
      i++;
    }
    else
    {
      state = IDLE;
    }
    break;
}

But like a lot of things it is a matter of one's own preferences

Polliwog:
Led one fades in, when it reaches max pwm (brightness), a second LED fades in while the first LED fades out.

If you mean that one LED should be bright when the other is dark then something simple like this should do the trick

void loop() {
   static byte ledAval = 0;
   static byte ledAchange = 10;
   byte ledBval = 255 - ledAval;
   digitalWrite(ledApin, ledAval);
   digitalWrite(ledBpin, ledBval);
      // check are we going to exceed the limits
   if ((ledAval + ledAchange ) < 0 or (ledAval + ledAchange) > 255) {
      ledaAchange =   - ledAchange;
    }
   ledAval += ledAchange;
   delay(50);
}

...R