I'm stuck for use loop functions for multi byte data.

i'm very beginning on arduino. I want to make to command loop of the 8 sensors with different address for sensorA to sensorH. I'm used command all of each 8 command duplicate only change byte dataA --> dataB --> dataC --...--->dataH than the program storage space are go very high. how can i should do for use byte type in one for loop

PS: I'm already use char alphabet[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; and then char to string and mix it. then it come out with that name not dataA that i want.

char alphabet[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void setup() {
  Serial.begin(9600);
  Serial.println("Start :");
}

void loop() {
  for(int j = 1; j < 9; j++) {
  byte dataA[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
  byte dataB[] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x38};
  byte dataC[] = {0x03, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0xE9};
  byte dataD[] = {0x04, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x5E};
  byte dataE[] = {0x05, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0x8F};
  byte dataF[] = {0x06, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0xBC};
  byte dataG[] = {0x07, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x6D};
  byte dataH[] = {0x08, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x92};
  Serial.println(j);
  Serial.println(alphabet[j]);
  Serial.println(data[j]);
  delay (2000);
  }
}

This code i'm try to make string name "dataA" from "j" but it is just "dataA" not the command byte type..

Thank you for Reading my problem and look forward for answer.

Your code as it exists does not compile. After fixing it so it would compile, i did not see any issues with memory, yet you did not specify what device you are running this on. On my Mega 2560 it only uses 2%.

I do question why you have your array initialization inside the loop, seems you would not want to initialize them more than once.

Also, arrays are zero based, please do not try to make them 1 based. It leads to confusion and makes it hard for others to help as they have to switch to a non-standard way of thinking.

I have read the description of what you are trying to do, perhaps i am tired this AM, but I after reading it several times, I simply could not get the concept of what you are trying to do. Perhaps someone can decipher what it is you are asking.

This code

for(int j = 1; j < 9; j++) {
  byte dataA[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
  byte dataB[] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x38};

will initialize the arrays 8 times. The initialization should not be inside the FOR loop. Even better would be to move it into setup() as it only needs to happen once. Even better would be to move it to the top of the program before setup() as it only needs to happen once and that way it will be available everywhere in the program.

This code

  for(int j = 1; j < 9; j++) {

      // .....

      Serial.println(data[j]);

will print the second to the 9th elements in the array called data[]. There are two problems with that, First there is no array named data[] (but there is one called dataA[]). Second the arrays only have 8 elements numbered from 0 to 7 - so it should be

for(int j = 0; j < 8; j++) {

and if you want to save memory it should be

for(byte j = 0; j < 8; j++) {

...R

EDITED to correct an error that was pointed out in Reply #4 by @Romonaga Apologies for any confusion

Robin2:
This code

  for(int j = 1; j < 9; j++) {

byte dataA[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
  byte dataB[] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x38};



will initialize the arrays 8 times. The initialization should not be inside the FOR loop. Even better would be to move it into setup() as it only needs to happen once.

Or do it at the top of the sketch, if you need access to it outside of setup.

Romonaga:
Or do it at the top of the sketch, if you need access to it outside of setup.

Stupid me. That is what I should have said.

Thanks for pointing it out. I have updated Reply #2

...R

First of all. Thank you for every each reply.

I get it. my logic is wrong about make the "NAME" change. I need to make array of all 8 byte array to make it one loop.

If i will find the way to compressed all of dataA[] to dataH[] to array of data[]

Reply #1 :Romonaga
My full sketch of duplicate 8 times used 16698 byte(51%) of Uno R3
i'm also need some more space for other sensors and real-times displays.

Reply #2 : Robin2
My mistake. I already move all of them to the top of the sketch. Thank you!

I'm looking forward to make it in 1 for loop of data[]

Best Regards.

SirMarx:
If i will find the way to compressed all of dataA[] to dataH[] to array of data[]

byte data[ 8 ][] =
{
0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B, // A
0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x38,
0x03, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0xE9,
0x04, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x5E,
0x05, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0x8F,
0x06, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0xBC,
0x07, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x6D,
0x08, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x92; // H
}

And so what was dataA[x] becomes data[ 0 ][x].

One name for the 2 dimensional array of arrays.

Yep, it work.
Thank you very much.

byte data[ 8 ][] =
{
  0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B, // A
  0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x38,
  0x03, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0xE9,
  0x04, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x5E,
  0x05, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0x8F,
  0x06, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC5, 0xBC,
  0x07, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x6D,
  0x08, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x92; // H
}

The name data all by itself is a const pointer (address and data type combined) to the base an 8 element array of const pointers to byte, the bases of 8 byte arrays.

When you index into an array, the first element is base + 0 which is why you start arrays with [ 0 ], it's the offset.