How to Parse an Array to a Function

Hello All!
While I have been tinkering with Arduino in a couple of formats for a while I have yet to make anything of note. I am new to this forum, although I trawl it frequently for answers to niggly questions as they have arisen and have always managed to get a clue that points me in the right direction. This time I can't seem to find an answer [ I have looked ], hence the post.

The code below [ for a hexapod robot] works as expected thus far, but I'm trying to eliminate a dozen or so lines of code per gait. Not convinced this code is what I would ultimately use and the array of data at this point is rubbish but understanding how to parse and array would be of huge benefit about now.

Apologies if I posted this in the wrong category.

Thanks in advance.

Drew

void Gait(int Steps)
{
    int g1a =  5; //array dimension 1, this will vary depending on the complexity of the Gait1
    int g1b = 12; //array dimension 2. this will vary depending on the complexity of the limbs
    
    int Gait1[g1a][g1b]{
        {1,1,1,1,1,1,1,1,1,1,1,1}, //six legs two servos per leg
        {2,2,2,2,2,2,2,2,2,2,2,2},
        {3,3,3,3,3,3,3,3,3,3,3,3},
        {4,4,4,4,4,4,4,4,4,4,4,4},
        {5,5,5,5,5,5,5,5,5,5,5,5}
    };

    //DoGait(g1a, g1b, Gait1[ ]) //HELP!!

// This is the code I would like to implement in DoGait() 
    for (int s=1; s<=Steps; s++) 
        {
            for ( int i = 0; i<=g1a-1; i++)
            {
                for ( int j = 0; j<=g1b-1; j++)
                {
                    Serial.println(Gait1[i][j]);
                    //individual servo work done here.
                }            
            }
        }
// to here.                       
}

void DoGait()
{
    
}

Welcome to the forum

What do you want to do with the data in the array ?

What do you mean by "parse"?

What sort of data does the array contain?

Hi.
The array data will be used in other functions I have written to actuate the servos in each leg.

The array is just integers.

You seem to be able to extract the values from the array and print them so where are you stuck ?

Since the array is a local variable, it will only be available to pass to functions called by the Gait() function. Is that your intent?

I'm just trying to eliminate some code and facilitate the possibility of multiple types of gaits.
It might end up looking something like this?

void Gait(int Steps)
  {
      int g1a =  5; //array dimension 1, this will vary depending on the complexity of the Gait1
      int g1b = 12; //array dimension 2. this will vary depending on the complexity of the limbs
      
      int Gait1[g1a][g1b]{
          {1,1,1,1,1,1,1,1,1,1,1,1}, //six legs two servos per leg
          {2,2,2,2,2,2,2,2,2,2,2,2},
          {3,3,3,3,3,3,3,3,3,3,3,3},
          {4,4,4,4,4,4,4,4,4,4,4,4},
          {5,5,5,5,5,5,5,5,5,5,5,5}
      };
  
      DoGait( g1a,  g1b,  Gait1[ ] ) //HELP!!
  
  }

void DoGait( the array parsed here )
  {
      for (int s=1; s<=Steps; s++) 
          {
              for ( int i = 0; i<=g1a-1; i++)
              {
                  for ( int j = 0; j<=g1b-1; j++)
                  {
                      Serial.println(Gait1[i][j]);
                      //individual servo work done here.
                  }            
              }
          }
  }

So, is what you really want to know is how to pass an array to a function ?

Yep, pretty sure that's what I need to know.

I want to separate the nested for loops into another function that will use the array data.

The point was, that will only work if the "another function" is called by Gait(). Will that be the case? Otherwise the array must be global.

So, if I define the array outside of a function, will that make it global? and usable to other functions?

I got no clue, but since you are learning, I point out or remind you that 0 is a perfectly good number, and for loops are typically written differently.

// here, readers take in at a glance 

for (int s = 0; s < Steps; s++) 

// as a loop of Steps number of iterations. Also, spaces are free and make code a bit friendlier

// and here again, g1a number of steps, no maths to swallow. 
 
              for ( int ii = 0; ii < g1a; ii++)

I replaced i with ii, pick your names but avoid single characters as symbols, even in a very local use. In wider scope, single letter names are hard to search for and find combing last all the 'i's that are in the source.

void DoGait(g1a, g1b, int someGait[])
{
    for (int ss = 0; ss < Steps; ss++) 
    {
        for ( int ii = 0; ii < g1a; ii++)
        {
            for ( int jj = 0; jj < g1b; jj++)
             {
                 Serial.println(someGait[ii][jj]);
                 //individual servo work done here.
              }            
          }
     }                
}

This does assume Ateps is global. You could pass that too as a parameter to you function/

I can't test it. It's close I will rely on sharp eyes to fix it if it's not correct somehiw.

a7

1 Like

And if you are on your way to defining multiple gaits gait1, gait2 and so forth, take a minute to see how that would work better as a three dimensional array, an array of two dimensional gates.

If all this numbers are small, using byte (0..255) or char (-128..127) instead of int will save space.

HTH

a7

1 Like

You know when people say "He's the man"? Today that's you!!
Many thanks.

Here is my take on it

byte gait1[2][2] = { { 1, 2 }, { 3, 4 } };
byte gait2[2][2] = { { 11, 12 }, { 13, 14 } };

void setup()
{
    Serial.begin(115200);
    aFunction(gait1);
    aFunction(gait2);
}

void loop()
{
}

void aFunction(byte theArray[2][2])
{
    for (byte row = 0; row < 2; row++)
    {
        for (byte col = 0; col < 2; col++)
        {
            Serial.print(theArray[row][col]);
            Serial.print("  ");
        }
        Serial.println();
    }
    Serial.println();
}
1 Like

No point in copying the array, make a reference. Since the function doesn't change the array, make it a constant reference:

void aFunction(const byte (&theArray)[2][2]) {
1 Like

Good point

So I can't fix my solution at the moment, and I don't know when the gait is a 2 x 2 array, as @drew64 says

    int g1a =  5; //array dimension 1, this will vary depending on the complexity of the Gait1
    int g1b = 12; //array dimension 2. this will vary depending on the complexity of the limbs

A trick would be to have the dimensions large enough for all gates, with each row terminated by a flag value indicating the lenth of a particular one, and the row after last row flagged at the beginning as a marker of the end of valid rows.

Then the loops across two dimensions woukd be

        for ( int ii = 0; someGait[ii][0] != 255; ii++)
        {
            for ( int jj = 0; someGait[ii][jj] != 255; jj++)
             {
                 Serial.print(someGait[ii][jj]);
                 Serial.print(", ");
              }
              Serial.println("");    
          }

The gait 2D array would be global, or as shown just now passed as a pointer to an array of certain dimensions.

a7