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.