I have a few arrays, some of them are float some of them az int (uint16_t, analog readings). I would like to write a method that can calsulate the mean value of all of them. Here is what I got right know. The problem is that the function can’t receive arrays with non floats.
#define NUM_OF_READINGS 5
uint16_t array1[NUM_OF_READINGS] = {1,2,3,4,5};
float array2[NUM_OF_READINGS] = {1,2,3,4,5};
uint16_t avg1;
float avg2;
void setup() {
Serial.begin(9600);
avg1 = average(array1);
avg2 = average(array2);
Serial.println(avg1);
Serial.println(avg2);
}
void loop() {
}
float average (float *array) {
float sum = 0L ;
for (int i = 0 ; i < NUM_OF_READINGS ; i++){
sum += array [i] ;
}
return ((float) sum) / NUM_OF_READINGS ;
}
Can I modify somehow the function, or do I need 2 functions for each types?