0

I'm trying to use a 2D array inside a struct on my arduino to store sensor readings. I'm having two main problems:

1: My Arduino is crashing and resetting when I try to access certain directories in my Array

2: My arraysize variable doesn't seem to be incrementing the way I'm expecting it to.

I've attached snippets of my code that I think are relevant.

struct calibration
{
  float NTU[];
  unsigned int readings[][28];
  unsigned int arraysize = 0;
};

//Main
void loop() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Waiting...");
  if (digitalRead(inputPin) == HIGH)
    TakeReadings();
  delay(50);
}


//after taking many readings from a sensor and averaging...

Calibration.readings[Calibration.arraysize][0] = average;
Calibration.arraysize++;
PrintMatrixToSerial();

//serial print function

void PrintMatrixToSerial() {
  Serial.print("1.9,1  3.8,1  7.5,1  15,1   30,1   60,1   120,1");
  Serial.println();
  for (int i=0; i<Calibration.arraysize; i++) {
    for (int j=0; j<7; j++) {
      Serial.print(Calibration.readings[i][j]); Serial.print("   ");
    }
    Serial.print('\n');
  }
}

When trying to assign values to my Calibration.readings array in certain directories, the Arduino hard crashes. For instance, a crash happens when the arduino tries to execute:

Calibration.readings[Calibration.arraysize][2] = average;

Secondly, after running my TakeReadings() function only once, I expect Calibration.arraysize to be equal to 1, therefore the PrintMatrixToSerial() function should only print one row of data; however, the serial monitor prints many rows. The following is an example of the output:

1.9,1 3.8,1 7.5,1 15,1 30,1 60,1 120,1

11 298 0 65292 779 1284 6
167 167 167 167 167 167 167
167 167 167 167 167 167 167
167 167 167 167 167 167 167
167 167 167 167 167 167 167
167 167 167 167 167 167 167
167 167 167 167 167 167 167
167 167 167 167 167 167 167
0 167 167 167 167 4245 40853
167 167 167 167 167 167 167
319 167 167 167 167 167 167

Is there an issue with my struct, or array declaration? I'm stumped at the moment, so any help would be appreciated.

5
  • You're not allocating any memory to your readings array. Give it a size. – Majenko Aug 2 '17 at 22:23
  • Thank you, that fixed both problems. I was under the impression that declaring the size of the first array in a matrix wasn't required, is that wrong for all cases? – Gtingstad Aug 2 '17 at 22:38
  • You can skip the first one IF AND ONLY IF you supply a list of initializers in braces as part of the definition. In that case the compiler will count the number of things in the list for you. – Delta_G Aug 2 '17 at 23:46
  • @Majenko - comments on questions are for clarifying the question. Please make a proper answer, especially as the system will throw this up as an unanswered question if you don't. – Nick Gammon Aug 3 '17 at 3:22
  • @NickGammon Many times I am using my phone and the android SE app. Writing answers on that is a real pain in the sphincter. So I get the OP up and running with a quick comment. Later on, when back at my PC (sometimes the next day) I convert my comments to a real answer. I do not need telling every time by you. I know how the site works. – Majenko Aug 3 '17 at 4:56
2

You are not specifying a size for your readings array:

unsigned int readings[][28];

That is not saying "Give me an array of an unknown number of chunks, each of 28 values", it is actually saying "Give me a pointer to a block of memory (which I will provide later) which is arranged in rows of 28 values".

In C you must either provide a size for an array, in which case the memory is statically allocated for you, or manually allocate the memory at a later date.

unsigned int readings[16][28]; // 16 rows of 28 int values

Or, for dynamic allocation:

unsigned int readings[][28];

// allocate the memory ...
blah.readings = malloc(16 * 28 * sizeof(int));
// do things with your array ...
// and free up the memory when finished:
free(blah.readings);

On an embedded system it is generally to know how many readings you will be taking beforehand. That way you can allocate the correct number of rows to the array before you compile the program, which makes many things more efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.