How to define byte array [ ] within a byte array [ ]

hello
I want to know about the merging or listing of multiple array within a mast array and how to call them when needed

a small example which is not actually the right one but a sample to explain what I need !!!

int dam [] = {1,2,3,4,5};
int bridge [] = {1,2,3,4,5};
int pool [] = {1,2,3,4,5};
int waterfall [] = {1,2,3,4,5};

byte tanker1
{

};

In above script
want to merge all the four arrays in byte tanker 1

1 Like

Do you mean you want a two-dimensional array, or a structure?

I'm sorry, but it doesn't help at all!

What does that mean?

Do you want just one long array consisting of the data from dam, followed by the data from bridge, etc ... ?

Perhaps explain how you intend to use this data?
What are you trying to achieve?

That doesn't make sense. Here is one way to do it but it depends on what you are trying to accomplish:

struct tanker_t
{
  int dam;
  int bridge;
  int pool;
  int waterfall;
};

tanker_t tanker1[] = 
  {{1,1,1,1},
   {2,2,2,2},
   {3,3,3,3},
   {4,4,4,4},
   {5,5,5,5}};
   
const int tanker1_size = sizeof(tanker1)/sizeof(tanker_t);
   
void setup()
{
  Serial.begin(15200);
  for (int i; i < tanker1_size; i++)
  {
    Serial.println(tanker1[i].dam);
    Serial.println(tanker1[i].bridge);
    Serial.println(tanker1[i].pool);
    Serial.println(tanker1[i].waterfall);
  }
}
   
void loop()
{     
}

As a 2D array?

int tanker1[][4] = {
   {1,2,3,4,5},  // dam
   {1,2,3,4,5},  // bridge
   {1,2,3,4,5},  // pool
   {1,2,3,4,5}   // waterfall
};
int *tanker1[] =
{
  dam, bridge, pool, waterfall
}
1 Like

Let's start with the difference between an array and a data structure.

An array contains only one data type. Because of this, any element can be accessed by a numerical index, i.e. unsigned integral types (byte, unsigned char, uint32_t). It is possible to access data outside of your array with an index larger than your array size.
A structure can contain multiple data types, but each element can only be accessed by name.
A structure can contain arrays mixed with other data types, and an array of data structures is possible.

Here is a 4-dimensional array:

byte gender;
byte birthMonth;
byte birthYear;
byte birthDay;
unsigned long birthDate[2][12][100][31];// this is 297600 bytes of RAM

setup() {
  Serial.begin(9600);
  gender = 1;// 0 or 1
  birthMonth = 5;// 0-11
  birthYear = 18;// 0-99
  birthDay = 20;// 0-30
  birthDate[gender][birthMonth][birthYear][birthDay] = millis();
}

loop() {
}

Ya something like that . .

let me explain in a way that should get things clear to everyone

consider three counter units for up and down operation
All those counter units has an individual variable which holds their individual value for three different functions
That function would be anything that requires multiple value for their operation
Anything it is . . . I ll try to give an example but first thing is the example code below

Here are 3 counters with different variables :-

FOR
int val1=1;

if(!digital(4))
{
if(val1<10)
{
val1=val1+1;
}
delay(200);
}

if(!digital(5))
{
if(val1>1)
{
val1=val1-1;
}
delay(200);
}

.
.
.
FOR
int val2=1;

if(!digital(4))
{
if(val2<10)
{
val2=val2+1;
}
delay(200);
}

if(!digital(5))
{
if(val2>1)
{
val2=val2-1;
}
delay(200);
}

.
.
.
FOR
int val3 = 1;

if(!digital(4))
{
if(val3<10)
{
val3=val3+1;
}
delay(200);
}

if(!digital(5))
{
if(val3>1)
{
val3=val3-1;
}
delay(200);
}

So now we have :-

val1
val2
val3

for 3 counter machine for particular function

Suppose now if we have a huge amount of functions that require huge amount of variables to define , say some 500 or 800 like that

then in that case we need to design that much amount of counters for all the variable because keeping a single variable say val1 ll effect all the counters with the very last value decided for a function
That value from that function ll get copied to other functions creating a complete wrong operation

I did hell of experiments with that and all failed . .

Now I wan to have this type of thing where a variable be allotted in place of a scaler quantity

Its something like :-
.
.
.

if(!digital(4))
{
if([   ]<10)
{
[   ]=[   ]+1;
}
delay(200);
}

if(!digital(5))
{
if([   ]>1)
{
[   ]=[   ]-1;
}
delay(200);
}

In this above code
We are going to define it in a function where in the place where bracket is there,
We are going to put
val 1
val 2
val 3
and so on

its something look like :-

void CounterMain ( byte [   ] )
{
if(!digital(4))
{
if([   ]<10)
{
[   ]=[   ]+1;
}
delay(200);
}

if(!digital(5))
{
if([   ]>1)
{
[   ]=[   ]-1;
}
delay(200);
}
}

Don't have much idea so explained in this ablove way
Rest my apologies !!!!

:man_shrugging: :woozy_face:

I see

Sir suppose I want to apply things with this one ??
in place of bracket below if I want to put
dam or waterfall or . . like that ??

void CounterMain ( [ ] )
{
if(!digital(4))
{
if([   ]<10)
{
[   ]=[   ]+1;
}
delay(200);
}

if(!digital(5))
{
if([   ]>1)
{
[   ]=[   ]-1;
}
delay(200);
}
}

:grin: :grin:

That is not a very big deal for all of you people

In simple sense its like we use scaler quantity to define a function like

void VAL(byte val1)
{
Serial.println(val1);
}

and its usage in void loop be like

val (2);

.
.
.
So in our case we need something like

void VAL (byte [ ] )
{
function inside;
}

and in void loop

VAL (val1);
or
VAL(val2);
or
VAL(val3);

!!!!!!!!!!!

OK Fine

how to apply the all those dam or bridge etc in the place of BRACKETS in below code so as to use it for variables ??

if(!digital(4))
{
if([   ]<10)
{
[   ]=[   ]+1;
}
delay(200);
}

if(!digital(5))
{
if([   ]>1)
{
[   ]=[   ]-1;
}
delay(200);
}

Do you just want a function that'll update a variable?

ie, you want a parameter that serves as both input and output?

void CounterMain ( int * pX  )
{
   if( !digital(4) )
   {
      if( *pX < 10 )
      {
         (*pX)++;
      }
   delay(200);
   }

   if( !digital(5) )
   {
      if( *pX > 1 )
      {
          (*pX)--;
      }
      delay(200);
   }
}

Here, the parameter pX is a pointer.

The function will update whatever that pointer points to

    CounterMain( &any_int_variable );

Will update the value of any_int_variable

Yeah Yeah something like that i think
I need to check

Well in that place of *pX , can I put directly the variable ??

You need to read-up on pointers!

No - you put the variable within the parentheses of the function call, as I showed.

You need to prefix the name with '&' - that is the operator to take the address of (ie, give a pointer to) a variable.

So, eg, to act on elements of your original arrays:

CounterMain( &dam[0] );        // update dam[0]

CounterMain( &bridge[1] );     // update bridge[1]

CounterMain( &pool[2] );       // update pool[2]

CounterMain( &waterfall[3] );  // update dam[3]

Ok le me check with that and ll then I ll post the final code with all these !!

You can define this:

void CounterMain ( int &val)  {
  if (val)
  ...
  val = val - 1;
}

and call it like

  CounterMain(val1);
  CounterMain(val2);
  ...

But if you have different calculations for val1, val2 and val3 then you have to write different functions.

No no same calculation for val1 or val 2 or val 3 are exactly the same :blush:
But have to apply for different functions
I mean with some set of input pins

Say Pin 22 has to do a function with some value say 6
likewise Pin 23 have to do another function with a different value say 4
likewise so many different functions with different push buttons
There we need to apply different variables but the counter system and counting
numbers are same
say 1 to 5
or
1 to 10
or
1 to 20

Whatever the range , ll be fixed for every variable that is defined in Array !!!

Back to #1 assume you have parameters like this

struct args {
  int pin, val1, val2, val3;
}
byte Anker1(&args) {
  if (!digital(args.pin)
  ...
}
1 Like

Yes - that would be better!
:+1:

That's using the C++ reference parameter type - which is a neater way of doing what I showed in old-school C
:older_adult: :older_man:

https://www.ibm.com/docs/en/zos/2.4.0?topic=calls-pass-by-reference-c-only

Well before proceeding with rest of the discussion in posts below
I would like to introduce the very first level of study

Here I tested it on 2 arrays of LEDs where each array has 4 set of LEDs

Applying the counter to array 1 selects an LED from pin 6 to pin 9 as mentioned in code below

and applying counter for array to selects LEDs from 10 to 13

For array 1
The selected LED gets triggered with Pin 22
For array 2
The selected LED gets triggered with Pin 23

All set !!!

Now as per the requirement with multiple variables to serve for different counters
the system is work well for 2 functions but . . .

A small issue is here : -

From both the arrays of LED packs as per rule we have positions 0,1,2,3
But at startup
The LED at the 1th position gets light Up on triggering
and counter starts from 1 then 2 then 3 but not from the 0th position

This thing happens with both the counters
I tested it one by one

int ledPins1 [] = {6,7,8,9};
int ledPins2 [] = {10,11,12,13,};

int dam [] = {1,2,3,4,5};
int bridge [] = {1,2,3,4,5};
int pool [] = {1,2,3,4,5};
int waterfall [] = {1,2,3,4,5};

int jar1 = 1;

int jug1 = 22;
int jug2 = 23;

int UpWards = 4;
int DownWards = 5;

int Sun = 1;

void setup() 
{
   Serial.begin(9600);
   pinMode(UpWards,INPUT_PULLUP);
   pinMode(DownWards,INPUT_PULLUP);
   pinMode(jug1,INPUT_PULLUP);
   pinMode(jug2,INPUT_PULLUP);
   pinMode(ledPins1, OUTPUT);
   pinMode(ledPins2, OUTPUT);
}

void loop() 
{

   // CounterMain( &dam[0] );        // update dam[0]

   // if(!digitalRead(jug1))
   //    {
   //        digitalWrite(ledPins1[dam[0]], HIGH);
   //        delay(100);
   //        digitalWrite(ledPins1[dam[0]], LOW);
   //        while(!digitalRead(jug1));
   //    }

   CounterMain( &bridge[0] );     // update bridge[1]

   if(!digitalRead(jug2))
        {
          digitalWrite(ledPins2[bridge[0]], HIGH);
          delay(100);
          digitalWrite(ledPins2[bridge[0]], LOW);
          while(!digitalRead(jug2));
        }





   

   // CounterMain( &pool[0] );       // update pool[2]

   // CounterMain( &waterfall[0] );  // update dam[3] 

   
}

void CounterMain ( int * pX  )
// void CounterMain ( int & val)
{
   if(!digitalRead(4))
   {
      if( *pX < 10 )
      {
         (*pX)++;
      }
   delay(200);
   }

   if(!digitalRead(5))
   {
      if( *pX > 1 )
      {
          (*pX)--;
      }
      delay(200);
   }
}

.
.
.

a lil. change I made here is putting zero everywhere

CounterMain( &dam[0] );        // update dam[0]

CounterMain( &bridge[0] );     // update bridge[0]

CounterMain( &pool[0] );       // update pool[0]

CounterMain( &waterfall[0] );  // update dam[0]