# Another Question - Accessing Varibles Dynamically

Hi once again,

I have been thinking of ways to optimize the code I am writing to ensure I do not go over the 32kb limitation, for example, I have 6 outputs (using the analog pins) in my software name Out1,Out2… and these are assigned values for when they are activated 0 if not at all, 1 if straight away and 2 if 1s later. For the actual activation I was going to use direct port manipulation so hence need to join this group of 6 0s and 1s and 2s into two commands, first activation and second activation. I know how to sort the values based on whether they are 1s or 2s quite easily.

Hence my two questions are is there a way I can access the value e.g. lcd.print(Out & 1) would be the value of Out1? My second question is similar but a different solution is there a way to create a string or number based on the values of out1 2 etc e.g. a = Out1 & Out2 & Out3
so that if out1 was 0, out2 was 2 and out3 was 1, a would equal 023, or would i just write each value as a char into a array which would then be read as a whole?

Callum Phenix

strings are slow, IMHO, so be careful:

``````int int1=1;
int int2=2;
int int3=3;
String myString = "";
myString += int1;
myString += int2;
myString += int3;
Serial.println(myString); //prints 123
``````

Are you familiar with array's? - http://arduino.cc/it/Reference/Array -
is that what you are looking for?

for example, I have 6 outputs (using the analog pins)

The analog pins are input only.

The bitRead(), bitSet(), bitClear(), and bitWrite() methods also bear looking at.

CallumP:
is there a way I can access the value e.g. lcd.print(Out & 1) would be the value of Out1?

Yes - use arrays. Whenever you find yourself putting numbers on the end of similar variable names, ask yourself whether your should treat them as an array.

CallumP:
is there a way to create a string or number based on the values of out1 2 etc e.g. a = Out1 & Out2 & Out3
so that if out1 was 0, out2 was 2 and out3 was 1, a would equal 023, or would i just write each value as a char into a array which would then be read as a whole?

You can either use sprintf, or just convert each number value to ascii and append it to a character array and then stick a null terminator on the end of the array to make a string.