3 LEDs fading - no for loop?

Hi,
I’m trying out a new code (for me) to pulse fade three LEDs at different intervals for each LED. I have previously made a code where i write code for each separate LED, but I’m now experimenting with ‘struct’ to define all three LEDs as blueleds(i) and fade using a for loop.

But the LEDs doesn’t fade. I’m assuming this is because I’m stuck in the for loop somehow, but I can’t see it :o Any suggestions on how to do this is appreciated.

#define blueLed1 9
#define blueLed2 10
#define blueLed3 11

#define blueLedcount 3   // set up the total number of Led in the Led set
#define UP 0             // fade direction
#define DOWN 1           // fade direction

struct blueled   
{                                           
  byte blueLedPin;                      
  byte blueLedfadedirection;          
  int blueLedVal;                     
  int blueLedfadeInterval;            
  unsigned long previousblueLedFadeMillis;   
};
                                
blueled blueleds[blueLedcount] = {{blueLed1, 0, 0, 5, 0}
                                 ,{blueLed2, 0, 0, 10, 0}
                                 ,{blueLed3, 0, 0, 15, 0}
};

const int debugInterval = 10;           // number of millisecs between serial prints, for debugging
unsigned long previuosdebug = 0;        // for debugging using serial

void setup() {     // the setup function runs once when you press reset or power the board
  
  Serial.begin(9600);
  Serial.println("Starting the LEDpulse sketch");  // to know what sketch is running
  pinMode(blueLed1, OUTPUT);
  pinMode(blueLed3, OUTPUT);
  pinMode(blueLed2, OUTPUT);

}

void loop() {
unsigned long currentMillis = millis();
blueLEDpulse(currentMillis);

//debug();                                 // serial print for debugging
}


void debug(){                  
      if (millis() - previuosdebug >= debugInterval) {
               Serial.print(blueleds[0].blueLedVal);    Serial.print("   ");
               Serial.print(blueleds[1].blueLedVal);    Serial.print("   ");
               Serial.println(blueleds[2].blueLedVal);  Serial.print("   ");
      previuosdebug += debugInterval;   }
}



void blueLEDpulse(unsigned long thisMillis) {
     for(int i = 0; i < blueLedcount; i++) {
              if (thisMillis - blueleds[i].previousblueLedFadeMillis >= blueleds[i].blueLedfadeInterval) {
                      if (blueleds[i].blueLedfadedirection == UP) {          // fading up
                      blueleds[i].blueLedVal += 1;
                      digitalWrite(blueleds[i].blueLedPin, 255 - blueleds[i].blueLedVal);
                             if (blueleds[i].blueLedVal >= 255) {   // At max, limit and change direction
                                  blueleds[i].blueLedVal = 255;
                                  blueleds[i].blueLedfadedirection = DOWN;
                             }
                      } else {                             // if not up, then down
                      blueleds[i].blueLedVal -= 1;
                      digitalWrite(blueleds[i].blueLedPin, 255 - blueleds[i].blueLedVal);  
                              if (blueleds[i].blueLedVal <= 0 ) {
                                   blueleds[i].blueLedVal = 0;        // At min, limit and change direction
                                   blueleds[i].blueLedfadedirection = UP;
                              }
                      }
                  blueleds[i].previousblueLedFadeMillis = thisMillis;   // reset millis for the next iteration
                } //end if thisMillis
     } //end for loop
}

This was my previous code for one of the LEDs:

// The third blue led set
void blueLED3pulse(unsigned long thisMillis) {
            if (thisMillis - previousblueLed3FadeMillis >= blueLed3fadeInterval) {
                                if (blueLED3fadeDirection == blueLed3UP) {          // fading up
                              blueLed3Val += 2;
                              if (blueLed3Val >= 255) {   // At max, limit and change direction
                                blueLed3Val = 255;
                                blueLED3fadeDirection = blueLed3DOWN;
                              }
                  } else {                             // if not up, then down
                              blueLed3Val -= 2;  
                              if (blueLed3Val <= 0 ) {
                                   blueLed3Val = 0;        // At min, limit and change direction
                                blueLED3fadeDirection = blueLed3UP;
                              }
                   }
                previousblueLed3FadeMillis = thisMillis;   // reset millis for the next iteration
            }

}

Thanks!

If you think that you are stuck in the for loop, use Serial.println(...) to print out the value of i and see what is happening. I do not necessarily agree with the diagnosis.

I do not know what this is supposed to do:

                      digitalWrite(blueleds[i].blueLedPin, 255 - blueleds[i].blueLedVal);

It appears twice. digitalWrite(...) is documented to write a HIGH or a LOW to a pin. I do not know that

255-blueleds[i].blueLedVal

is going to be the value HIGH or LOW.

Perhaps you meant analogWrite(...).

vaj4088: Thanks for your reply. While trying a new way in the code i did a lot of copy and paste and, as you pointed out, it's supposed to be analogWrite(...). It works now! Thanks. Another pair of eyes did the trick :slight_smile: