Variable variables?

Hi all,

Is it possible to use variable variables? If so, what is the correct syntax?

If anyone is not familiar with the term, it’s when you have a variable name which you can change programatically - in PHP you do something like this:

$var1 = "a";
$var2 = "b";
$var3 = "c";

for ($i = 1; $i <= 3; $i++) 
{
   $mydynamicvar = "var".$i;    
   echo $mydynamicvar;
}

This would return “abc”

Is there a way of doing this in c on the Arduino?

Thanks!

You could probably do something like this with pointers, arrays, and/or structs - which is probably how PHP does it under the hood. I have never seen something like this for the Arduino, though, nor do I think it is possible to do this "natively" in C/C++

:)

Thank you cr0sh

If anyone is able to show me a way to do this, I'd be very grateful. My c is very, very rusty, and was never great in the first place :-)

Maybe if you tell us what it is you want to achieve, rather than how you want to do it?

Ok, good point :)

I want to be able to loop through all the output pins, or blocks of the output pins, either turning them on or off, with various delays in the loop.

For that, simple arrays will suffice, I believe.

const int N_PINS  10
const int pinDelays [N_PINS] = {1, 1200,  //yadda yadda};
const byte pinNumbers [N_PINS] = {1, 2, 3, 6, 7, 9, 4...};//whatever
bool pinVals [N_PINS];
//
//
// then a simple "for" loop
for  (int i = 0 i < N_PINS; ++i ) {
  delay (pinDelays [i]);
  pinVals [i] = digitalRead (pinNumbers [i]);
}

Neat, thank you :)

Now I can finally make my evil light chaser and take over the world. Mwahaha.

I'm still going to miss variable variables though...

Thanks for your help.

Before you get real thrilled with that code, beware that delay() stops anything else from happening (except interrupts).

The "blink without delay" example shows how to achieve the same results without using the delay() function.

Depending on what you want the Arduino doing while a particular LED is on, the delay() function may, or may not, get in the way.

Once you learn how to use it, the next step is generally to learn how not to use it.

Ah, I see - ok, thank you, I'll read up more on delay().

variable variables?

Could someone explain to this old hardware guy what that means? Are not variables already variables? ;)

What am I missing?

Lefty

What am I missing?

You're missing out on a whole new world of stress and pain - believe me, you're better off without 'em.

Sure, I’ll have a go. What happens if you want to refer to a variable, but you want to generate the name of that variable on the fly, by looping through a series for example?

Well, in the magical land of PHP (where I hail from), we do this:

$var1 = "a";
$var2 = "b";
$var3 = "c";

for ($i = 1; $i <= 3; $i++)
{
   $mydynamicvar = "var".$i;    
   echo $mydynamicvar;
}

In that loop, by adding an extra $ to $mydynamicvar, we’re saying “don’t just tell me what the string in $mydynamicvar is, use that string as a variable name, and tell me what that variable’s value is”.

So it returns the contents of $var1, $var2 and $var3 - “abc”

Does that make sense?

So it returns the contents of $var1, $var2 and $var3 - "abc"

Does that make sense?

Sort or, thanks. Not that I've been spending my life looking for such a feature. ;)

Lefty

I don't know how anyone can live without it!

I don't know how anyone can live without it!

One uses other nifty tricks, such as: Pointers, objects and arrays.

C and C++, being compiled rather than interpreted languages, don't have the ability for the application to access the symbol namespace used at compile time, so they don't do this sort of thing. By the time the binary code is loaded into the AVR, all your variables have become nothing more than memory references, and the names of the variables are no longer even present.

I want to be able to loop through all the output pins, or blocks of the output pins, either turning them on or off, with various delays in the loop.

byte oddpins[] = {1,3,5,7,9,11,13,-1};
byte evenpins[] ={2,4,6,8,10,12,-1};
void digitalWrite_multiple(byte *pinlist, byte state)
{
  while (*pinlist != -1) { // loop through pins in the set, stopping at -1
    digitalWrite(*pinlist, state);
    pinlist++;
}
void setup()
{
   digitalWrite_multiple(oddpins, HIGH);
   digitalWrite_multiple(evenpins, LOW);
}

If you byte a -1 it will bite you in the ass.