Hello everyone
I wrote this sketch to learn and test how to send a multidimensional array to a function and return the average:
float total = 0;
int numReadings = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // for debugging
float test [3][6] {
{7.5, 3.6, 4.7, 2.8, 2.9, 1.2 }, // 22.7, average 3.78
{2.1, 1.2, 7.2, 6.8, 1.9, 9.2 }, // 28.4, average 4.73
{1.5, 8.6, 3.7, 5.8, 3.9, 9.2 } // 32.7, average 5.45
};
for ( int i = 0; i < 3; i++) {
float resultArray[i];
resultArray[i] = averageMath(test);
Serial.print("result ");
Serial.print(i);
Serial.print(": ");
Serial.println(resultArray[i]);
}//for
}//setup
float averageMath(float Readings[3][6]){
for (int i = 0; i < 3; i++){
static float average[3];
total = 0;
for (int j = 0; j < 6; j++ ){
total = total + Readings[i][j];
Serial.print("total ");
Serial.print(i);
Serial.print("-");
Serial.print(j);
Serial.print(": ");
Serial.println(total);
}//for j
average[i] = total / 6;
Serial.print("average ");
Serial.print(i);
Serial.print(": ");
Serial.println(average[i]);
return average[i];
}//for i
}//averageMath
void loop() {
// put your main code here, to run repeatedly:
}
So far everything worked perfectly.
Then I wanted to make it more general to be able to send what ever mulidimensional array. I found this site to give me an idea on how to do it:
I wanted to work with method 2.
I ended with this code:
float total = 0;
int numReadings = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // for debugging
int m = 3;
int n = 6;
float test [m][n] {
{7.5, 3.6, 4.7, 2.8, 2.9, 1.2 }, // 22.7, average 3.78
{2.1, 1.2, 7.2, 6.8, 1.9, 9.2 }, // 28.4, average 4.73
{1.5, 8.6, 3.7, 5.8, 3.9, 9.2 } // 32.7, average 5.45
};
for ( int i = 0; i < m; i++) {
float resultArray[i];
resultArray[i] = averageMath((float*)test, m, n);
Serial.print("result ");
Serial.print(i);
Serial.print(": ");
Serial.println(resultArray[i]);
}//for
}//setup
float averageMath((float *)readings, int a, int b){
// static float average[3];
for (int i = 0; i < a; i++){
static float average[3];
total = 0;
for (int j = 0; j < b; j++ ){
total = total + readings[i][j];
Serial.print("total ");
Serial.print(i);
Serial.print("-");
Serial.print(j);
Serial.print(": ");
Serial.println(total);
}//for j
average[i] = total / b;
Serial.print("average ");
Serial.print(i);
Serial.print(": ");
Serial.println(average[i]);
return average[i];
}//for i
}//averageMath
void loop() {
// put your main code here, to run repeatedly:
}
As to my understanding I just changed the parameters for the sizes of the arrays and I changed the variable Readings to readings. But now all of a sudden I get the "averageMath was not declared in this scope" error:
Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Uno"
R:\Arduino\Terrarium\0_Test_Average_OneArray_2_bidimen_pointers\0_Test_Average_OneArray_2_bidimen_pointers.ino: In function 'void setup()':
0_Test_Average_OneArray_2_bidimen_pointers:19:50: error: 'averageMath' was not declared in this scope
resultArray[i] = averageMath((float*)test, m, n);
^
R:\Arduino\Terrarium\0_Test_Average_OneArray_2_bidimen_pointers\0_Test_Average_OneArray_2_bidimen_pointers.ino: At global scope:
0_Test_Average_OneArray_2_bidimen_pointers:27:28: error: 'readings' was not declared in this scope
float averageMath((float *)readings, int a, int b){
^
0_Test_Average_OneArray_2_bidimen_pointers:27:38: error: expected primary-expression before 'int'
float averageMath((float *)readings, int a, int b){
^
0_Test_Average_OneArray_2_bidimen_pointers:27:45: error: expected primary-expression before 'int'
float averageMath((float *)readings, int a, int b){
^
0_Test_Average_OneArray_2_bidimen_pointers:27:51: error: expected ',' or ';' before '{' token
float averageMath((float *)readings, int a, int b){
^
exit status 1
'averageMath' was not declared in this scope
Invalid library found in R:\Arduino\IDE\libraries\libraries: no headers files (.h) found in R:\Arduino\IDE\libraries\libraries
Invalid library found in R:\Arduino\IDE\libraries\Terrarium_forum: no headers files (.h) found in R:\Arduino\IDE\libraries\Terrarium_forum
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I looked through the different functions and I could not find why this error occurs.
Thank you all for the help.
moses