Calling a variable name that is based on another variable's value

Hello, I'm fairly new to this, but have searched the internet far and wide without joy, so I will ask you good people for help.

I am attempting to call the value of a variable whose name will change based on the value of another variable.

int cs = 0;
int s1 = 0; int s2 = 0; int s3 = 0; int s4 = 0; int s5 = 0; int s6 = 0; int s7 = 0; int s8 = 0; int s9 = 0; int s10 = 0; int s11 = 0; int s12 = 0; int s13 = 0; int s14 = 0;

void setup() {
s1 = (128); cid = 9876;
cs = 1;
}

void loop(){
//Decide which slave to poll
if (cs > 14){ cs = 1;}

cs = SelectSlave();
}

int SelectSlave() {

if ("s" + cs & 128 >= 128){ return cs;}
cs = (cs + 1);
SelectSlave();
}

With this bit "s" + cs I am attempting to get the value of s1, with the 1 coming from the value of cs. When cs changes, the value compared will also change to that of s2, s4 etc.

I hope I've explained this clearly enough. Thanks in advance.

Hi,

You need to look up 'array' and figure out how that might help

Duane B

Thanks for the pointer Duane

Duane, thanks for replying.

I've read up on arrays, and I guess I could store the data currently housed in s1 - s14 in either one large or fourteen smaller arrays. There would be fourteen rows and eight columns, which according to what I have read, would use a lot more memory than 14 ints at 2x10 to the power of 7.

Regardless though, I would still need to call data from an array based on the value of an int. I don't see a way around this using arrays. Maybe I'm not expressing the problem well, or maybe I'm a bit slow. Either are possible.

If I used an array and wanted to refer to a value within it based on it's position in the array, the digit that represents the position would still have to come from the int cs. Please correct me if I'm wrong....

Yes, you can store the data currently in s1 through to s14 in an array:

int s [14];

That takes exactly the same amount of memory, and now you can "index" into the array (bear in mind the first one is index 0, not index 1).

m35oz:
If I used an array and wanted to refer to a value within it based on it's position in the array, the digit that represents the position would still have to come from the int cs. Please correct me if I'm wrong....

Make cs go from 0 to 13, rather than 1 to 14. Then:

foo = s [cs];

Can't get much simpler than that.

Nick, thanks for replying.

foo = s

;


won't compile. Error is

SlavePollingExperimental:30: error: invalid types 'int[int]' for array subscript

Doesn't seem to like an int's name being called from the value of another int. I've been googling all afternoon to no avail.

Post the whole code.

int cs = 1;/current slave/ int tpf = 0; /Current Slave Trunk poll fail count/
int mpf = 4; int cid = 0; int debug = 0; int wt = 0;
int anydata = 217; int at = 129; int ad = 65; int bt = 17; int bd = 9; int reset = 33; int tdata = 135; int pollstring = 0; int pollreply = 0; int s = 0; int foo = 0;
int s1 = 0; int s2 = 0; int s3 = 0; int s4 = 0; int s5 = 0; int s6 = 0; int s7 = 0; int s8 = 0; int s9 = 0; int s10 = 0; int s11 = 0; int s12 = 0; int s13 = 0; int s14 = 0; //these s values when popelated will be 2x10 / 7

void setup() {
Serial3.begin(9600); UCSR3C = UCSR3C | B00000110;/* Send an output to a PC for event log 8 n 1 / Serial1.begin(300); UCSR1C = UCSR1C | B00001110; /Slave polling. 8 n 2/ Serial2.begin(9600); UCSR2C = UCSR2C | B00000110;/ Send an output to a PC for debug 8 n 1 */
s1 = (128); cid = 9876; debug = 2000;//delete this line after web server data retrieval set up
Serial2.print(" System Reboot "); Serial2.print("CID "); Serial2.println(cid); //write a system restart to the event log
}

void loop(){
if (debug > 0){delay(debug); Serial2.println("Debug is enabled so we are putting a delay between each cycle of");Serial2.println(debug);Serial2.println(" ms");} delay(50);

//Decide which slave to poll
if (cs > 14){ cs = 1; Serial2.println("Slave number was too high so we have reset the current slave to 1. CS = "+ cs);} Serial2.println("Slave number was between 1 and 14 so we skip to Poll.Slave");//Make sure we are polling a slave that has a chance of existing. If not, go back to Slave 1

cs = SelectSlave();

}
int SelectSlave() {
foo = s

;

if (foo & 128 >=> 128){Serial2.print("Slave ");Serial2.[color=#CC6600]print/color; Serial2.println(" exists so we are going to Poll.Slave");  return cs;}
else {Serial2.print("Slave ");Serial2.[color=#CC6600]print/color;Serial2.print(" didn't exist so we move to the next one"); cs = (cs + 1);//Make sure the current slave exists ie is enabled in sn
SelectSlave();}
}

[/quote]

You need to define an array before you can use it.

http://arduino.cc/en/Reference/Array

Lots of problems with this code. I recommend that you get a basic book or website on C programming and work through the exercises. Function calls and arrays are fundamental concepts - you really have to understand them at least a little to write new code. Reading other arduino sketches can also help.

An array needs to be declared.

int s[14];

You can then remove s1 thru s14, and foo = s[cs]; will work. You don't need
to set cs to 1, it should start with 0. Then in setup, s1 = (128); becomes

s[0] = 128;

The rest of the problems are in the function SelectSlave.
First, if (foo & 128 >= 128) looks weird. foo & 128 will only ever have two values: 0 or 128. So it should look like if (foo & 128 == 128) or even just if (foo & 128).

The way you have written it, the function SelectSlave is recursive: it calls itself. I don't think that's what you meant to do. Currently, it will also has no stop condition, so sometimes it will
recurse forever, or at least until there is no more memory. You need a loop to find a good value for cs.

while ( s[cs] & 128 == 0) {
  cs++;
  if (cs > 13)
      cs = 0;
}

There is still the problem of what to do if none of the s values are good. You should think about how to handle that.

Anyway, I hope this will get you pointed in the right direction.