Pages: [1]   Go Down
Author Topic: Passing int array to loop() from setup()  (Read 675 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Newbie
*
Karma: 1
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Thanks!

Mike
Logged

Canada
Offline Offline
Newbie
*
Karma: 1
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
  Serial.begin(9600);
  // 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");
  Serial.println();
 // 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 ");
      Serial.print(nn);
      Serial.print(pos[nn][0]);
      Serial.print(pos[nn][1]);*/
      nn++;
    }
  }
  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]);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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().
Logged

Canada
Offline Offline
Newbie
*
Karma: 1
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: