Hi All,
I'm a long time reader, first time poster. I appreciate the help that I've found throughout the forum so thank you ahead of time. The concept seems very simplistic to me so I put together a quick sketch to understand how 2d arrays work better on the Arduino platform. I'm currently working with a Yun and my end goal is to log readings from the analog inputs to the on board SD card. Unfortunately I'm running into some issues that I'm hoping are obvious to others. I have some programming experience but am new to embedded micro controllers hence the attempt at learning the basic functionality before moving on.
The goal here is to populate a 2d array by running through 2 for loops and then regurgitate the array in the serial array. I've placed a lot of print lines in to try to debug it but something doesn't seem to be clicking.
Here's the sketch:
int data_array[5][1];
int data;
unsigned long time_ref = 0;
unsigned long time_taken = 0;
bool flag;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while(!Serial); // wait for Serial port to connect.
flag = 0;
}
void loop() {
// put your main code here, to run repeatedly:k = 0;
if (flag == 0){
Serial.println("Start");
int k = 0;
Serial.print("millis() is "); Serial.println(millis());
time_ref = millis();
Serial.print("Time is "); Serial.println(time_ref);
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 1; j++)
{
data_array[i][j] = k;
Serial.print("i = "); Serial.println(i);
Serial.print("j = "); Serial.println(j);
Serial.print("k = "); Serial.println(k);
Serial.print("The i j matrix is "); Serial.println(data_array[i][j]);
k++;
}
}
time_taken = millis() - time_ref ;
Serial.print("This operation took "); Serial.println(time_taken);
Serial.print("millis() is "); Serial.println(millis());
delay(500);
time_ref = millis();
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 1; j++)
{
data = data_array[i][j];
Serial.print("i = "); Serial.println(i);
Serial.print("j = "); Serial.println(j);
Serial.print("The i j matrix is "); Serial.println(data_array[i][j]);
}
}
time_taken = millis() - time_ref;
Serial.print("This operation took "); Serial.println(time_taken);
flag = 1;
}
}
There are many issues with this which can be seen in the following output which I copied from the serial comm window.
Issues:
- When k = 10 the i j matrix value is 269
- The time calculation doesn't make any sense "This operation took 4294966801"
- When reposting the array to serial the numbers don't match what was input was
- The array spots [1,4] and [0,5] just look like jibberish
I really hope the solution is obvious and thank you again in advance! Any suggestions on future post (format etc.) would be appreciated as well.
Start
millis() is 3313
Time is 3313
i = 0
j = 0
k = 0
The i j matrix is 0
i = 0
j = 1
k = 1
The i j matrix is 1
i = 1
j = 0
k = 2
The i j matrix is 2
i = 1
j = 1
k = 3
The i j matrix is 3
i = 2
j = 0
k = 4
The i j matrix is 4
i = 2
j = 1
k = 5
The i j matrix is 5
i = 3
j = 0
k = 6
The i j matrix is 6
i = 3
j = 1
k = 7
The i j matrix is 7
i = 4
j = 0
k = 8
The i j matrix is 8
i = 4
j = 1
k = 9
The i j matrix is 9
i = 5
j = 0
k = 10
The i j matrix is 269
i = 5
j = 1
k = 11
The i j matrix is 11
This operation took 4294966801
millis() is 2818
i = 0
j = 0
The i j matrix is 0
i = 0
j = 1
The i j matrix is 2
i = 1
j = 0
The i j matrix is 2
i = 1
j = 1
The i j matrix is 4
i = 2
j = 0
The i j matrix is 4
i = 2
j = 1
The i j matrix is 6
i = 3
j = 0
The i j matrix is 6
i = 3
j = 1
The i j matrix is 8
i = 4
j = 0
The i j matrix is 8
i = 4
j = 1
The i j matrix is -1416
i = 5
j = 0
The i j matrix is -1416
i = 5
j = 1
The i j matrix is 12
This operation took 5