A for loop for a method call (?)

I’ve installed this library that handles buttons and leds easily (GitHub - robertgallup/BobaBlox: Simple hardware library for Arduino (button, knob, led, speaker, photocell).)
My question is: if I have multiple leds named led1, led2, …, ledn.
How can I call this methods in a for loop?

For example how can I do something like this (which I tried unsuccessfuly):

for (int i = 1; int < 10; i++){
  led[i].on;
}

Here is the example code for a single LED

////////////////////////////////////////////
//  
//  BobaBlox Example Sketch: LED
//  
//  DECLARATION:
//
//  LED myLED(pin)				// pin is LED pin
//
//  METHODS:
//
//  void on ();
//  void off ();
//  void set (state);				// Sets LED on if state is greater than zero
//  void blink ();				// Blinks LED once with default 250ms on and off
//  void blink (n);				// Blinks n times with default blink delay
//  void blink (n, delay);			// Blinks n times with specific delay
//  void blink (n, delayON, delayOFF);		// Blinks n times with delayON and delayOFF
//  void setBrightness (brightness);		// On an Analog Output (PWM) pin, sets LED brightness (0-255)
//
////////////////////////////////////////////

#include <BobaBlox.h>

// Declarations
LED boardLED(1);

// Setup runs once
void setup() {                
}

// Loop repeats forever
void loop() {

  // Blinks LED. Uncomment one of the blinks below
  // Uncomment one of the lines below for it's effect
  boardLED.blink();
  // boardLED.blink(1, 100);
  // boardLED.blink(1, 20, 250);


}
// put these in the appropriate spots
// pre setup()
ledPinsArray[] = {2,3,4,5,6,7,8,9,10,11};

in setup()

for {x=0; x<10, x=x+1){
pinMode (ledPinsArray[x], OUTPUT);
}

// in loop()
// use same for loop syntax as above
boardLED.blink(edPinsArray[x], 100);

Thanks for the answer @CrossRoads

I think this is not how the library works

For starters, it doesn’t require the setup of the pins the setup loop. You do that with

LED led1(10);

led1 being of the class LED, and 10 the pin number

The other thing is that .blink() or .on() doesn’t accept any parameters. If I want to turn on 3 leds it would be like

led1.on();
led2.on();
.
.
.
ledn.on();

that is why i need something in the form of

luispotro:

for (int i = 1; int < 10; i++){

led[i].on;
}

I am not sure about C++, but in C you could just create an array of function pointers, and call those.

https://isocpp.org/wiki/faq/pointers-to-members

As I understand it the library is defective as any globally constructed LED calls 'pinMode' and 'digitalWire' BEFORE the Arduino API is intialized in 'main'.

It's in 'main' where the Arduino API is initialized AFTER all globals have been intialized undoing anything done in the construction of the global.

LED::LED (byte p)
{
    _pin = p;
    pinMode (_pin, OUTPUT);
    digitalWrite (_pin, LOW);
}

Should you decide to continue with the library the code should be something like the following -

#include <BobaBlox.h>

#define ENTRIES_COUNT(ARRAY)        (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinLED_1    =  1;
const uint8_t   pinLED_2    = 13;
const uint8_t   pinLED_3    = YOUR_NEXt_LED_In_CONNECTION_HERE;
const uint8_t   pinLED_4    = YOUR_NEXt_LED_In_CONNECTION_HERE;

const uint8_t   aLEDS[]     =
{
      LED(pinLED_1)
    , LED(pinLED_2)
    , LED(pinLED_3)
    , LED(pinLED_4)
};

// Declarations
LED boardLED(1);

void setup()
{
    for ( size_t i = ENTRIES_COUNT(aLEDS); i--; )
    {
        aLEDS[i].on();
    }
}

void loop()
{   }

lloyddean:
Should you decide to continue with the library the code should be something like the following -

#include <BobaBlox.h>

#define ENTRIES_COUNT(ARRAY)        (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t  pinLED_1    =  1;
const uint8_t  pinLED_2    = 13;
const uint8_t  pinLED_3    = YOUR_NEXt_LED_In_CONNECTION_HERE;
const uint8_t  pinLED_4    = YOUR_NEXt_LED_In_CONNECTION_HERE;

const uint8_t  aLEDS    =
{
      LED(pinLED_1)
    , LED(pinLED_2)
    , LED(pinLED_3)
    , LED(pinLED_4)
};

// Declarations
LED boardLED(1);

void setup()
{
    for ( size_t i = ENTRIES_COUNT(aLEDS); i–; )
    {
        aLEDS[i].on();
    }
}

void loop()
{  }

It fails to compile with

exit status 1
cannot convert ‘LED’ to ‘const int’ in initialization

exit status 1
cannot convert ‘LED’ to ‘const uint8_t {aka const unsigned char}’ in initialization

Changed ...

const uint8_t   pinLED_1    =  1;

... to ...

uint8_t   pinLED_1    =  1;
... SAME WITH OTHERS...

As I said the library in question is defective

exit status 1
cannot convert 'LED' to 'uint8_t {aka unsigned char}' in initialization

I guess I'll follow your advice on using defective libraries...

LED is a type. It is NOT a uint8_t, and you cannot convert LED to uint8_t. Your aLEDs array needs to be of type LED, not uint8_t.

Regards, Ray L.

Correct. I’m apparently to tired to continue today as I’m making simple mistakes!!!

KeithRB:
I am not sure about C++, but in C you could just create an array of function pointers, and call those.

Standard C++

yup…

try something like this, which I cannot compile but I think it looks okay…

#include <BobaBlox.h>

// Declarations
LED boardLED(1);

LED* myLED[3];           // create array of pointers to instances of class
byte leds[3] = {2,3,4};  // array of pins to which the array of pointers uses

// Setup runs once
void setup() 
{
  for (byte i = 0; i < sizeof(myLED)/sizeof(myLED[0]); i++)
  {
    myLED[i] = new LED(leds[i]);  // use new to construct the instances
  }  
}

// Loop repeats forever
void loop() 
{
  for (byte i = 0; i < sizeof(myLED)/sizeof(myLED[0]); i++)
  {
    myLED[i]->blink();  // dereference the pointer and perform the function
  } 
  
  boardLED.blink();
}

uncompiled, untested.

EDIT: COMPILES, TESTED.