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