Variable Names in Variables.

I am trying to run a sequence of LEDs on a charlieplex setup using the charlieplex library. My though is to reduce the amount of code to run a chase into a loop instead of coding each digitalWrite and delay.
I am trying to change the variable name passed to charlieplexWrite by using an array. However I get an error.
“error: no matching function for call to 'Charlieplex::charlieWrite(char&, int)”…
How can I set a variable to equal a variable name?

//Charlie Plex setup

#define NUMBER_OF_PINS 3
byte pins = {4,5,6} ; //physical connections
Charlieplex charlieplex = Charlieplex(pins,NUMBER_OF_PINS);
//individual ‘pins’ , address charlieplex pins as you would address an array
charliePin led1 = { 1 , 0 }; //led1 is indicated by current flow from
charliePin led2 = { 0 , 1 }; //led2 is indicated by current flow from
charliePin led3 = { 0 , 2 }; //led3 is indicated by current flow from
charliePin led4 = { 2 , 1 }; //led4 is indicated by current flow from
charliePin led5 = { 2 , 0 }; //led5 is indicated by current flow from
charliePin led6 = { 1 , 2 }; //led6 is indicated by current flow from
char led = {‘led1’, ‘led2’, ‘led3’, ‘led4’, ‘led5’, ‘led6’}; //array of LED variable names

void setup() {

//ready to run led sequence
for (int thisLed = 1; thisLed < 7; thisLed++) {
charlieplex.clear();
charlieplex.charlieWrite(led[thisLed],HIGH);
delay(50);
}
}

First off, strings in C are enclosed in double-quote marks (“string”). Only individual chars are enclosed in single-quote marks (‘A’).

But that’s really not what you want to be doing anyway. When you call a function such as charlieWrite(led1, 1) it is not passing the name of the variable (ie “led1”) to the function, it is passing the variable’s contents (technically, depending on the data type, it could be the contents or a pointer to the contents).

The error you’re seeing:
“error: no matching function for call to 'Charlieplex::charlieWrite(char&, int)”
means that you’re trying to pass a char string, but no function in the library is designed to accept that data type as a parameter.

Secondly, your “for” loop should be in the “void loop()” function rather than the “void setup()” function.

Now to a solution:

I forget some of the C++ stuff, so I’m going to be guessing a bit on this.

I think what you want to do is:

//Charlie Plex setup

#define NUMBER_OF_PINS 3
byte pins[] = {4,5,6} ; //physical connections
Charlieplex charlieplex = Charlieplex(pins,NUMBER_OF_PINS);
//individual 'pins' , address charlieplex pins as you would address an array
charliePin leds[6];  // create an array of type charliePin


void setup() {

// setup values of each charliePin
    leds[0] = { 1 , 0 }; //led1 is indicated by current flow from
    leds[1] = { 0 , 1 }; //led2 is indicated by current flow from
    leds[2] = { 0 , 2 }; //led3 is indicated by current flow from
    leds[3] = { 2 , 1 }; //led4 is indicated by current flow from
    leds[4] = { 2 , 0 }; //led5 is indicated by current flow from
    leds[5] = { 1 , 2 }; //led6 is indicated by current flow from
}


void loop() {
 
 //ready to run led sequence
 for (int thisLed = [glow]0[/glow]; thisLed < [glow]6[/glow]; thisLed++) {
 charlieplex.clear();  
 charlieplex.charlieWrite(leds[thisLed],HIGH);
 delay(50);
 }
}

Only individual chars are enclosed in single-quote marks

Don't forget multi-character constants like unsigned long alpha = 'abcd';

How can I set a variable to equal a variable name?

//Charlie Plex setup
#define NUMBER_OF_PINS 3
byte pins[NUMBER_OF_PINS] = {4,5,6} ; //physical connections
Charlieplex charlieplex = Charlieplex(pins,NUMBER_OF_PINS);

//individual 'pins' , address charlieplex pins as you would address an array
charliePin led1 = { 1 , 0 }; //led1 is indicated by current flow from 
charliePin led2 = { 0 , 1 }; //led2 is indicated by current flow from 
charliePin led3 = { 0 , 2 }; //led3 is indicated by current flow from 
charliePin led4 = { 2 , 1 }; //led4 is indicated by current flow from 
charliePin led5 = { 2 , 0 }; //led5 is indicated by current flow from 
charliePin led6 = { 1 , 2 }; //led6 is indicated by current flow from 
charliePin* led[] = {&led1, &led2, &led3, &led4, &led5, &led6};  //array of LED variable names

//later in code
charlieplex.charlieWrite(*led[0],HIGH);

:)

Grove.... I wasn't aware of multicharacter constants, but with a little research it seems that while that is valid, it is also implementation dependent. So different compilers might treat it differently. Seems like it's best to avoid unless you really know what you're doing and don't need to worry about code portability.

Wiskow.... What I forgot to mention, the highlighted numbers in the "for" loop: array indexes in C start at 0 not at 1. So if you declare an array as leds[6], then it contains 6 data elements that are indexed as 0 through 5. So your original code would have skipped the first element, and read some random value from memory on the last loop.

Thank you, I will give the code a try. BTW I am putting a "for" in "void setup()" because I want the code to run once when the unit is turned on. Unless I find a better way.

I am trying to under stand AlphaBeta's code. the "&" is an AND bitwise operation right? "*" is multiplication operation?

The “&” in this case means “reference to” or “address of”. So it creates an array where each element is a pointer to a variable. That is, the array element isn’t the variable itself, but tells you where in memory to find the variable.

The “*” means to “dereference” the pointer – that is, follow the pointer to the actual variable.

So this way you create the variables: led1, led2, etc.
Then create an array which contains pointers to the led1, led2, etc.
So “leds[0]” is a pointer to led1. But the charleWrite function wants led1, so “*leds[0]” tells it to pass the value pointed to by leds[0].

Yeah, pointers are confusing & a pain, but also very useful.