HowTo factorise class declaration with array?

Hello

Sorry for the bad english
I have 8x8 display LED and I use LedControl.h library.

Normal instance declaration is :

#include "LedControl.h"

LedControl lc=LedControl(pin1,pin2,pin3,1);

void setup() {
    lc.shutdown(0,false);    
    lc.setIntensity(0,4);  
}
loop ....

I use several independant LED blocks.

Code run fine with something like :

#include "LedControl.h"
/*
LedControl lc=LedControl(data,clk,CS,1);
DIN  22,23,24,25,26 
CLK  28,29,30,31,32
CS 34,35,36,37,38
*/

LedControl lc=LedControl(22,28,34,1);
LedControl lc2=LedControl(23,29,35,1);
LedControl lc3=LedControl(24,30,36,1);
LedControl lc4=LedControl(25,31,37,1);
LedControl lc5=LedControl(26,32,38,1);

void setup() {
    lc.shutdown(0,false);    
    lc.setIntensity(0,4);    
    lc2.shutdown(0,false);    
    lc2.setIntensity(0,4);    
    lc3.shutdown(0,false);    
    lc3.setIntensity(0,4);    
    lc4.shutdown(0,false);    
    lc4.setIntensity(0,4);    
    lc5.shutdown(0,false);    
    lc5.setIntensity(0,4);    
}

loop...

How to factorise statement
LedControl lc=LedControl(22,28,34,1); ?

A "for" loop is not possible outside setup() or loop()

I tried also something like this

 LedControl lc[0]=LedControl(22,28,34,1);
 LedControl lc[1]=LedControl(23,29,35,1);
 ...

I got "conflicting declaration 'LedControl lc [1]' "

We have the same problem to solve with other kind of classes, with multiple devices to drive...

Maybe an idea ?

Hello

What do you mean by "factorise" ?

If you want an array of LedControl objects you can do this

LedControl lc[] =
{
  LedControl(22,28,34,1),
  LedControl(23,29,35,1),
  LedControl(24,30,36,1),
  LedControl(25,31,37,1),
  LedControl(26,32,38,1),
};
1 Like

Exactly what you show ! Sorry for poor english.
That will reduce many lines further in the sketch !

I'm a strong user of PHP language, but beginer with arduino IDE and I'm ofter lost with it. But I learn every day.
Your help was usefull.
Many thanks !

Have a good day

Is just a set of tools, the language is c++ which you should be learning

1 Like