Adressing variable using a string and an int

I wonder how I can address a variable by putting together a string and an int in the Arduino development environment. I´m using many variables of the same type and with almost the same name. I´m just append a number to each variable. The names of the variables are e.g. int sensorValue_1; int sensorValue_2; and so on. I´d like to write less because my code is becoming too long. When addressing the variables, I´d like to write something like this: sensorValue_[+ intVariable];

Here is an example of what I mean:

int sensorIndex_1 = 1;
int sensorIndex_2 = 2;
int sensorIndex_3 = 3;

int sensorValue_1;
int sensorValue_2;
int sensorValue_3;


void setup()
{
  Serial.begin(9600);
}

void loop()
{
  doSomething(sensorIndex_1);
//doSomething(sensorIndex_2);
//doSomething(sensorIndex_3);
}

void doSomething(int sensorIndex)
{
  if(sensorIndex == 1)
  {
    Serial.print("Sensor 1: ");
    sensorValue_1 = analogRead(A1);
    Serial.println(sensorValue_1);
  }

  if(sensorIndex == 2)
  {
    Serial.print("Sensor 2: ");
    sensorValue_2 = analogRead(A2);
    Serial.println(sensorValue_2);
  }

  if(sensorIndex == 3)
  {
    Serial.print("Sensor 3: ");
    sensorValue_3 = analogRead(A3);
    Serial.println(sensorValue_3);
  }

  delay(1000);
}

I want to shorten the code in the doSomething() method. I want to have something like this: Notice the “[+ sensorIndex]”

void doSomething(int sensorIndex)
{
  Serial.print("Sensor [+ sensorIndex]: ");
  sensorValue_[+ sensorIndex] = analogRead(A[+ sensorIndex]);
  Serial.println(sensorValue_[+ sensorIndex]);

  delay(1000);
}

By the way: I´d like to avoid for-loops, if possible. In my case, the code would become too complicated.
How do I manage this?

It sounds to me like you need to read all about arrays.

Give this a read:
http://blogs.agilefaqs.com/2008/11/08/self-documenting-code-example/

AWOL:
It sounds to me like you need to read all about arrays.

I know what arrays are, don´t worry :slight_smile:
However my original code has too many arrays. No more array please!

mrburnette:
Give this a read:
» Self Documenting Code Example » Managed Chaos by Naresh Jain

Well, this is not what I´m looking for.
I want to construct the name of a variable by using multiple components:

sensorValue_1

is made up of

sensorValue_

and

sensorIndex

In particular, I want to insert an int into a string.
Any idea?
Let me know!

I want to construct the name of a variable by using multiple component

The names of variables in C have been lost by the time the code runs, so your WIBNI will just remain just that.

No more array please!

I don't understand your reluctance.

AWOL:
The names of variables in C have been lost by the time the code runs, so your WIBNI will just remain just that.

Well, my variables are general variables. I declared them just before the setup() function.
I don´t understand why I shouldn´t be able to speak to the variable by contructing the name of it (e.g. sensorValue_1) !!
The "1" should be the content of an int.
(??)

Well, my variables are general variables.

It doesn't matter what type of variables, global, static, automatic, whatever, their names simply do not exist at runtime.
End of story.

Quit hand-waving -
" I´d like to avoid for-loops, if possible. In my case, the code would become too complicated."
"However my original code has too many arrays. No more array please!"
" I want to have something like this:"

Tell us what you want to achieve, not how you think you ought to implement it.

I don´t understand why I shouldn´t be able to speak to the variable by contructing the name of it (e.g. sensorValue_1) !!

I don't understand why your reference can't simply be sensorValue [x]

Hmmm, I´m afraid there´s indeed no way past arrays!
So I´m trying to fix it using arrays and for-loops…
i hope I´m coping with 100000 for-loops in one code. What a fun!
But thanks for your help!
Now I know what´s NOT possible :stuck_out_tongue:
Rock on!

i hope I´m coping with 100000 for-loops in one code. What a fun!

And what did you envisage the alternative as being?

I wonder how I can address a variable by putting together a string and an int in the…

Goodness! I seem to remember that dbaseIII and maybe Clipper could pull such things off with what were called “code blocks” … I think it is something cavemen used :astonished:

Arduino has a sophisticated C++ backend compiler. One one of the passes, Symbols and Variables are converted into addresses in flash memory. So, as AWOL stated, once compiled variable names are “locked” as are functions, namespace, variable scope, etc.
IF one were interested in such things, this may be helpful: http://www.network-theory.co.uk/docs/gccintro/gccintro_90.html

Everytime you compile under Arduino, there is a directory (Windows) where temporary files are kept, in the Microsoft world this environmental variable is %temp% and you can go off there and check-out the ELF file and show the assembly.

Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:>CD %temp%

Directory of C:\Users\owner\AppData\Local\Temp\build3444858020490814565.tmp

07/29/2013 02:38 PM 13,496 Blink.cpp.elf
1 File(s) 13,496 bytes

Total Files Listed:
1 File(s) 13,496 bytes
0 Dir(s) 103,332,007,936 bytes free

cd build3444858020490814565.tmp
prompt $g
>dir *.elf
Directory of C:\Users\owner\AppData\Local\Temp\build3444858020490814565.tmp

07/29/2013 02:38 PM 13,496 Blink.cpp.elf
1 File(s) 13,496 bytes
0 Dir(s) 103,330,881,536 bytes free

avr-objdump -d Blink.cpp.elf >> listing.txt

notepad listing.txt

PARTIAL LISTING ONLY:

Blink.cpp.elf:     file format elf32-avr


Disassembly of section .text:

00000000 <__vectors>:
   0:	0c 94 61 00 	jmp	0xc2	; 0xc2 <__ctors_end>
  40:	0c 94 9d 00 	jmp	0x13a	; 0x13a <__vector_16>
 
00000068 <port_to_mode_PGM>:
  68:	00 00 00 00 24 00 27 00 2a 00                       ....$.'.*.

00000072 <port_to_output_PGM>:
  72:	00 00 00 00 25 00 28 00 2b 00                       ....%.(.+.

0000007c <port_to_input_PGM>:
  7c:	00 00 00 00 23 00 26 00 29 00                       ....#.&.).

00000086 <digital_pin_to_port_PGM>:
  86:	04 04 04 04 04 04 04 04 02 02 02 02 02 02 03 03     ................
  96:	03 03 03 03                                         ....

0000009a <digital_pin_to_bit_mask_PGM>:
  9a:	01 02 04 08 10 20 40 80 01 02 04 08 10 20 01 02     ..... @...... ..
  aa:	04 08 10 20                                         ... 

000000ae <digital_pin_to_timer_PGM>:
  ae:	00 00 00 07 00 02 01 00 00 03 04 06 00 00 00 00     ................
  be:	00 00 00 00                                         ....

000000c2 <__ctors_end>:
  c2:	11 24       	eor	r1, r1
  c4:	1f be       	out	0x3f, r1	; 63
  c6:	cf ef       	ldi	r28, 0xFF	; 255
  c8:	d8 e0       	ldi	r29, 0x08	; 8
  ca:	de bf       	out	0x3e, r29	; 62
  cc:	cd bf       	out	0x3d, r28	; 61

000000ce <__do_copy_data>:
  ce:	11 e0       	ldi	r17, 0x01	; 1
  d0:	a0 e0       	ldi	r26, 0x00	; 0
  d2:	b1 e0       	ldi	r27, 0x01	; 1
  d4:	e8 e3       	ldi	r30, 0x38	; 56
  d6:	f4 e0       	ldi	r31, 0x04	; 4
  d8:	02 c0       	rjmp	.+4      	; 0xde <.do_copy_data_start>

000000da <.do_copy_data_loop>:
  da:	05 90       	lpm	r0, Z+
  dc:	0d 92       	st	X+, r0

000000de <.do_copy_data_start>:
  de:	a2 30       	cpi	r26, 0x02	; 2
  e0:	b1 07       	cpc	r27, r17
  e2:	d9 f7       	brne	.-10     	; 0xda <.do_copy_data_loop>

000000e4 <__do_clear_bss>:
  e4:	11 e0       	ldi	r17, 0x01	; 1
  e6:	a2 e0       	ldi	r26, 0x02	; 2
  e8:	b1 e0       	ldi	r27, 0x01	; 1
  ea:	01 c0       	rjmp	.+2      	; 0xee <.do_clear_bss_start>

000000ec <.do_clear_bss_loop>:
  ec:	1d 92       	st	X+, r1

000000ee <.do_clear_bss_start>:
  ee:	ab 30       	cpi	r26, 0x0B	; 11
  f0:	b1 07       	cpc	r27, r17
  f2:	e1 f7       	brne	.-8      	; 0xec <.do_clear_bss_loop>
  f4:	0e 94 0b 02 	call	0x416	; 0x416 <main>
  f8:	0c 94 1a 02 	jmp	0x434	; 0x434 <_exit>

000000fc <__bad_interrupt>:
  fc:	0c 94 00 00 	jmp	0	; 0x0 <__vectors>

00000100 <loop>:
 100:	80 91 00 01 	lds	r24, 0x0100
 104:	61 e0       	ldi	r22, 0x01	; 1
 106:	0e 94 d8 01 	call	0x3b0	; 0x3b0 <digitalWrite>
 10a:	68 ee       	ldi	r22, 0xE8	; 232
 10c:	73 e0       	ldi	r23, 0x03	; 3
 10e:	80 e0       	ldi	r24, 0x00	; 0
 110:	90 e0       	ldi	r25, 0x00	; 0
 112:	0e 94 0b 01 	call	0x216	; 0x216 <delay>
 116:	80 91 00 01 	lds	r24, 0x0100
 11a:	60 e0       	ldi	r22, 0x00	; 0
 11c:	0e 94 d8 01 	call	0x3b0	; 0x3b0 <digitalWrite>
 120:	68 ee       	ldi	r22, 0xE8	; 232
 122:	73 e0       	ldi	r23, 0x03	; 3
 124:	80 e0       	ldi	r24, 0x00	; 0
 126:	90 e0       	ldi	r25, 0x00	; 0
 128:	0e 94 0b 01 	call	0x216	; 0x216 <delay>
 12c:	08 95       	ret

0000012e <setup>:
 12e:	80 91 00 01 	lds	r24, 0x0100
 132:	61 e0       	ldi	r22, 0x01	; 1
 134:	0e 94 6c 01 	call	0x2d8	; 0x2d8 <pinMode>
 138:	08 95       	ret

........................................ <... deleted section ...>..............................................................
...
00000434 <_exit>:
 434:	f8 94       	cli

00000436 <__stop_program>:
 436:	ff cf       	rjmp	.-2      	; 0x436 <__stop_program>
int sensorIndex_1 = 1;
int sensorIndex_2 = 2;
int sensorIndex_3 = 3;

int sensorValue_1;
int sensorValue_2;
int sensorValue_3;

Can become:

int sensorIndex [3] = { 1, 2, 3 };

int sensorValue [3];

Isn't that simpler?

IF one were interested in such things, this may be helpful: http://www.network-theory.co.uk/docs/gccintro/gccintro_90.html

This doesn´t make the code simpler at all :stuck_out_tongue:

Isn't that simpler?

Yes, as mentioned, I´ll construct the code using arrays.
Actually I wanted to avoid arrays in arrays in arrays ........ (in my case)
Obviously that´s still the simplest way! :slight_smile: