Learning Arrays

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:

  1. When k = 10 the i j matrix value is 269
  2. The time calculation doesn't make any sense "This operation took 4294966801"
  3. When reposting the array to serial the numbers don't match what was input was
  4. 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

int data_array[5][1]; Creates an array to hold just 5 individual numbers (yes just 5) Why bother having the second dimention if it is just 1 in size?

** int k = 0;** I don't see this changing anywhere else in your programme

The only value you ever assign to any of your array members is K Since K is always zero, that's what you'll see when you print them

When an array should contain two numbers, it should be declared with '2', and the index [ 0 ] and index [ 1 ] are used. That is how the c-language is defined.

int myArray[2];

myArray[0] = 21;
myArray[1] = 335;

When 100 of those pairs of numbers are needed, it is like this:

int myPairs[100][2];

for (int i=0; i<100; i++)    // index 0...99
{
  for (int j=0; j<2; j++)    // index 0...1
  {
    myPairs[i][j] = 6;
  }
}

Check your sketch, fix it and test it. If anything is not okay, show us your new sketch.

While I was writing this, KenF wrote the same.

You have loops that iterate 6 and 2 times respectively, yet have declared an array with 5 x 1
entries. If you want a 6x2 array, declare it thus.

Hi Everybody,

Thanks for the quick replies. Case closed, I should have looked at the details of how to declare an array in C/Arduino and was thinking more like VBA. I made the changes to the array size and all is good. Silly mistake and a simple fix, thanks again!

Ryan

You're welcome