# how to capture multiple coordinate datas in an array

Hi, I am currently working on a project of micromanipulator using 3 stepper motors. One of the functions of the machine is to record the route of the movement and to shuttle between two working coordinates following the route I designated. I want to have a two dimentional array to store the sets of coordinates between the two working coordinates, i.e. (x0, y0, z0, 0), (x1,y1,z1,1)… (xn,yn,zn,n), if the routes takes n steps. What I want to do is to set the step number as n, using an interrupt funtion to record the coordinates coresponding to each step once the interrupt pin is triggered:
The program that I imagined looks something like this:

int n = 0;
int coordinates[n][4]
coordinate_record()
{
coordinates[4][n] = {x,y,z,n}
n++
}

In this way, I want to generate an n row, 4 column two dimentional array that store the route. However, I find it is not easy to manipulate array in C++ than doing so in python. I watched the tutorials on youtube on manipulation of arrays but found out that to populate arrays, there are two ways:

either you can populate an empty array all in once, like:

coordinates[5][4] = {{5,7,9,0},{1,3,5,1},{2,4,7,2},{5,8,1,3}}

or to use the for loop to read in each element one by one like this:

int coordintates[5][4]
for(int i=0; i<5; i++){
for(int j=0; j<4; j++){
coordinates*[j] = values*
}
I want to read in the coordinate of each step one by one to make the two dimentional array shown above, like:
coordinates[0][4] = {5,7,9,0};
coordinates[1][4] = {1,3,5,1};
coordinates[2][4] = {2,4,7,2};
coordinates[3][4] = {5,8,1,3};
Could anyone help me to solve the problem? Thank you.

Would it simplify things to create a struct for a single measurement and then have an array of structs - something like

``````struct PositionStruct {
byte xVal;
byte yVal;
byte zVal;
};

PositionStruct arrayOfPositions[10];
``````

Then you can refer to an individual value with

``````arrayOfPositions[n].xVal = 123;
``````

...R

Hi Robin, thank you so much for your suggestions. I also got suggestions from others that I should use struct. So I will do some homework for this.

Robin2:
Would it simplify things to create a struct for a single measurement and then have an array of structs - something like

``````struct PositionStruct {
``````

byte xVal;
byte yVal;
byte zVal;
};

PositionStruct arrayOfPositions[10];

``````

Then you can refer to an individual value with

``````

arrayOfPositions[n].xVal = 123;

``````

...R
``````

Hi Robin, I tried this method and find the definition is confined within a function. I wrote the following code:

#include <Arduino.h>
struct PositionStruct{
int xVal;
int yVal;
int zVal;
};
void setup(){
Serial.begin(9600);
PositionStruct arrayofPositions[10];
arrayofPositions[0].xVal = 123;
arrayofPositions[0].yVal = 456;
arrayofPositions[0].zVal = 789;
arrayofPositions[1].xVal = 500;
arrayofPositions[1].yVal = 600;
arrayofPositions[1].zVal = 700;
arrayofPositions[2].xVal = 100;
arrayofPositions[2].yVal = 200;
arrayofPositions[2].zVal = 300;
printCoordinate();
}
void loop(){
}
void printCoordinate(){
for(int i = 0; i< 10; i++){
Serial.println(arrayofPositions*.xVal);*
_ Serial.println(arrayofPositions*.yVal);_
_ Serial.println(arrayofPositions.zVal);
}
}
The arduino IDE report:
Coordinate:25: error: ‘arrayofPositions’ was not declared in this scope
Serial.println(arrayofPositions.xVal);
^
exit status 1
‘arrayofPositions’ was not declared in this scope*

However, if I move the Serial.println staff in the void setup() function, it works fine(like the code I pasted below. So could you tell me what is the problem?
#include <Arduino.h>
struct PositionStruct{
* int xVal;
int yVal;
int zVal;
};
void setup(){
Serial.begin(9600);
PositionStruct arrayofPositions[10];
arrayofPositions[0].xVal = 123;
arrayofPositions[0].yVal = 456;
arrayofPositions[0].zVal = 789;
arrayofPositions[1].xVal = 500;
arrayofPositions[1].yVal = 600;
arrayofPositions[1].zVal = 700;
arrayofPositions[2].xVal = 100;
arrayofPositions[2].yVal = 200;
arrayofPositions[2].zVal = 300;
for(int i = 0; i< 10; i++){
Serial.println(arrayofPositions.xVal);
Serial.println(arrayofPositions.yVal);
Serial.println(arrayofPositions.zVal);
}
}
void loop(){
}*_

This

``````for(int i = 0; i< 10; i++){
Serial.println(arrayofPositions.xVal);
Serial.println(arrayofPositions.yVal);
Serial.println(arrayofPositions.zVal);
}
``````

should be

``````for(int i = 0; i< 10; i++){
Serial.println(arrayofPositions[i].xVal);
Serial.println(arrayofPositions[i].yVal);
Serial.println(arrayofPositions[i].zVal);
}
``````

And see how there are no italics when you use the code tag </> to display code

...R