Fading led's in sequence not together.

Hi

This is my first arduino project that i am trying to learn to do myself and not some pre assembled thing. What I am trying to do is to fade the led one after another not together. And I can't seem to manage to do that. here is my code.

#include <math.h>
unsigned long previousMillis = 0; // last time update
long interval = 2000; // interval at which to do something (milliseconds)

void setup()
{
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(11, val);
  if(currentMillis - previousMillis > interval) {
     previousMillis = currentMillis;
  analogWrite(11, 0);
  }
  if(currentMillis - previousMillis > interval) {
     previousMillis = currentMillis;
  analogWrite(10, val);
  }
}

Make your loop so that only fade action is done at a time when this comparison is true

if(currentMillis - previousMillis > interval) {

like this ?

#include <math.h>

unsigned long previousMillis = 0; // last time update
long interval = 2000; // interval at which to do something (milliseconds)

void setup()
{
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(11, val);
  if(currentMillis - previousMillis > interval) {
    analogWrite(11, 0);
    previousMillis = currentMillis;
  
  }
  //if(currentMillis - previousMillis > interval) {
     //previousMillis = currentMillis;
  analogWrite(10, val);
  
}

No, only do the fading when the millis timer times out like CrossRoads said. You have not done this.

I was thinking more like this concept

void loop(){
currentMillis = millis();
if (currentMillis - previousMillis >= interval){
previousMillis = previousMillis + interval;
led_to_fade = led_to_fade+1;
if (led_to_fade== max){led_to_fade = 0;}  // reset pointer, say max = 3 for this example
// float calculation
switch(led_to_fade){
case 0:
:
:
break;
case 1:
:
:
break;
case 2:
:
:
break;
} // end switch
} // end time check
} // end loop

I think that confused me more I tried something like this but i am pretty sure its wrong.

#include <math.h>

unsigned long previousMillis = 0; // last time update
long interval = 2000; // interval at which to do something (milliseconds)

int led1 = 11;
int led2 = 10;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop(){
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval){
    previousMillis = previousMillis + interval;
    led1 = led1+1;
    if (led1== 3){led1 = 3;}  // reset pointer, say max = 3 for this example
   
    float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
    
switch(led1){
case 0:
analogWrite(led1, val);

break;
case 1:
analogWrite(led2, val);

} // end switch
} // end time check
} // end loop

You are recreating the variable currentMillis every time the loop is execuited so it can never time out.