Declaring array of objects

I have a class called LedBlinky. Its function to blink an LED given a pin, off duration and on duraration.

The constructor of the class is something like this:

LedBlinky(char port, int pin, int onDur, int offDur) {
  // some codes
}

I am able to initialize the object individually as shown below:

LedBlinky led1('D', 5, 750, 350);
LedBlinky led2('D', 6, 400, 600);

void setup() {
  // empty
}

void loop() {
  led1.Run();
  led2.Run();
}

However, I would like to have an array of object to initialize multiple LEDs. Below is my attempts with different approaches:

Attempt 1

LedBlinky leds[8] = {
  LedBlinky('D', 0, 781, 515);
  LedBlinky('D', 1, 2014, 1348);
  LedBlinky('D', 2, 343, 573);
  LedBlinky('D', 3, 678, 1389);
  LedBlinky('D', 4, 342, 534);
  LedBlinky('D', 5, 1478, 326);
  LedBlinky('D', 6, 1859, 351);
  LedBlinky('D', 7, 777, 888);
};

void setup() {
  // put your setup code here, to run once:
  // led = ('D', 0, 781, 515);
  
}

void loop() {
  for (int i = 0; i < leds.length; i++) {
    leds[i].Run();
  }
}

Error:

ex_9\ex_9.ino:76:30: error: expected '}' before ';' token
   LedBlinky('D', 0, 781, 515);
                              ^
ex_9\ex_9.ino:76:30: error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'LedBlinky'
D:\Iqmal\Documents\mcte4342-embedded-system-design\Week4\ex_9\ex_9.ino:77:13: error: expected unqualified-id before 'D'
   LedBlinky('D', 1, 2014, 1348);

etc.

Attempt 2

Full code here.

LedBlinky leds[8];

void setup() {
  // put your setup code here, to run once:
  leds[0] = LedBlinky('D', 0, 781, 515);
  leds[1] = LedBlinky('D', 1, 2014, 1348);
  leds[2] = LedBlinky('D', 2, 343, 573);
  leds[3] = LedBlinky('D', 3, 678, 1389);
  leds[4] = LedBlinky('D', 4, 342, 534);
  leds[5] = LedBlinky('D', 5, 1478, 326);
  leds[6] = LedBlinky('D', 6, 1859, 351);
  leds[7] = LedBlinky('D', 7, 777, 888);
  
}

void loop() {
  for (int i = 0; i < sizeof(leds); i++) {
    leds[i].Run();
  }
}

Error:

ex_9\ex_9.ino:75:17: error: no matching function for call to 'LedBlinky::LedBlinky()'
 LedBlinky leds[8];
                 ^
ex_9\ex_9.ino:27:3: note: candidate: LedBlinky::LedBlinky(char, int, int, int)
   LedBlinky(char port, int pin, int onDur, int offDur) {
   ^~~~~~~~~
ex_9\ex_9.ino:27:3: note:   candidate expects 4 arguments, 0 provided
ex_9.ino:16:7: note: candidate: constexpr LedBlinky::LedBlinky(const LedBlinky&)
 class LedBlinky {
       ^~~~~~~~~
ex_9\ex_9.ino:16:7: note:   candidate expects 1 argument, 0 provided
ex_9\ex_9.ino:16:7: note: candidate: constexpr LedBlinky::LedBlinky(LedBlinky&&)
ex_9.ino:16:7: note:   candidate expects 1 argument, 0 provided

exit status 1

Compilation error: no matching function for call to 'LedBlinky::LedBlinky()'

I seems to can't point out what is the problem is. Any helps are highly appreciated

In the array, change ';' to ','

1 Like

Thanks! Wonder how I missed that one :man_facepalming:

It happens.

Note you can also write it this way and let the compiler calculate the size for you to get a more generic code.

LedBlinky leds[] = {
  {'D', 0,  781,  515 },
  {'D', 1, 2014, 1348 },
  {'D', 2,  343,  573 },
  {'D', 3,  678, 1389 },
  {'D', 4,  342,  534 },
  {'D', 5, 1478,  326 },
  {'D', 6, 1859,  351 },
  {'D', 7,  777,  888 },
};
const byte ledCount = sizeof leds / sizeof *leds;

Note that for (int i = 0; i < sizeof(leds); i++) { won’t go through the array, it’s the wrong size, you need to divide the number of bytes used up by the array by the number of bytes used up by one element in the array to get the count. So with the constant

for (int i = 0; i < ledCount; i++) …

Also you can use the range based for loop in c++ to enumerate the array elements , for example to call run() on each led

for (auto& aLed : leds) aLed.run()  // usually functions start with a lower case 

auto just tells the compiler to figure out the type. The & is to have a reference to the original element in the array

3 Likes

@fareezmewo

Can you please, post a complete class/object based sketch that will keep executing Led1.Run() function in every two seconds interval to blink (On period 750 ms, Off-period 350 ms) the LED connected with DPin-5 of Port-D?

@GolamMostafa
Here you go: mcte4342-embedded-system-design/ex_7.ino at main · iqfareez/mcte4342-embedded-system-design (github.com)

1 Like

Thanks! TIL about the auto stuff.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.