Passing int array to loop() from setup()

Hi all!

I'm making a simple uC program to perform the Cartesian product and output the results for me. Currently the way I have it set up is that you input your x and y values in the setup() function, and it builds the Cartesian set (size : x*y by 2 array)

For those of you unfamiliar with the Cartesian Product: Wiki : Cartesian product

Anyhow, once that array is built, I want to pass it to my main function so that it displays the next value of the set everytime it receives a digital trigger on one of the lines.

My question to you is how do you pass an array from a function to the other? I've no experience in C++ and my function keeps giving me errors. I've asked a few friends, and they said that you can't pass arrays around, and it must be done through pointers. I can't make the variable global as I don't know the size of x and y before starting it.



const int ao_x = 5; // Analog Output Pin, for chan x
const int ao_y = 6; // Analog Output Pin, for chan y
const int trig = 10; // Digital Input, for trigger line
int nn = 0; // Counter for number of digital triggers received

void setup() {
  // Open serial communications:
  // Initialise variables:
  double x1, x2, y1, y2;
  unsigned int nx, ny;
  // Get the values TEMP
  Serial.println("Enter the values for x1, y1, x2, y2, nx, ny");
 // Temporairly set them until get input working later
  x1 = 0;
  y1 = 0;
  x2 = 1;
  y2 = 10;
  nx = 3;
  ny = 11;
  // Cartesian product
  double pos[nx*ny][2], ytemp;
  int nn = 0;
  for(int jj=0; jj<ny; jj++)
    ytemp = y1+jj*(y2-y1)/(ny-1);
    for(int ii=0; ii<nx; ii++)
      pos[nn][0] = x1+ii*(x2-x1)/(nx-1);
      pos[nn][1] = ytemp;
      // Display results of Cartesian Product
      /* Serial.println("loop ");
  analogWrite(ao_x, pos[nn][0]);
  analogWrite(ao_y, pos[nn][1]);
  Serial.print("Done! Ready for trigger\n");

void loop() {
  // TODO: If trigger in, then increment nn

  // Write x and y chanels 
  analogWrite(ao_x, *pos[nn][0]);
  analogWrite(ao_y, *pos[nn][1]);

My question to you is how do you pass an array from a function to the other?

Passing a function to an array is not the difficult part. The setup() function does not call loop(), though, so you can not make setup() pass an array to loop().

You can define the array globally (before setup()), so that both functions have access to the same array. Initialize it in setup(), and use it in loop().

Hi Paul,

Thanks for your answer. So when I declare it globally, how can I do it? Since I dont know its size, I get size undefined errors. If I just define it as a simple double, and not a array of double, only the first value is available in main()

If you truly want to create a variable sized array, you'll need to look at using malloc. The pointer to the (pointers to the) space that you allocate must still be global. The Arduino has a limited amount of memory, so dynamic memory allocation is generally to be avoided.

Creating a static array large enough for your most ambitious use is better, as long as that array fits in memory, with enough room left to run the rest of your program.