calling a running Avg function in a for-loop

Hey guys and gals,

i'm new here and also fairly new to programming (cpp is my first language)
i have been fiddling with some basic stuff in the past but am now trying to make a functional program for a home made amplifier to be used with an arduino board.

so this code is a teaching project but primarily functional.

I have done quite a bit already but i'm stuck and can't seem to solve the issue, which should be rather basic but isn't to me.

i've gracefully been handed a solution as a library which i could use but i want to understand the task rather than using a black box.

i will post a code sniblett below. this is NOT the entire code... but should be enough.

the idea is the read from a couple of sensors (lm35's) the temp.
the way it is handled is important to me..

i need to read all sensors in a row and each reading of the sensor needs to be put in the first (0) position of there own running average array.
after the sensors been read and that loop is completed. it needs to loop again. and place that reading in the second position (1). till the array is full and well following the idea of the running average.

so what i don,t want is to full the running average array with 10 consecutive readings from one sensor.. but this should be obvious.

my problem is that i have the for loop ready, i have the running average ready but i don't understand how to (modify the running average code to so i can loop through it like i mentioned above. i should be close but i keep getting compiling errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const int  tempSens1        = A6;  // the output pin of LM35-1
const int  tempSens2        = A7;  // the output pin of LM35-2

const byte N_sensors        = 2;   // number of sensors

#define    RunAvgArr      10       // size running average array.

// global var
uint8_t   AnalogPinArray[N_sensors]= {tempSens1, tempSens2};
uint8_t   temp[N_sensors];         // array of temp per analog port


// main function
int main () {
  
  // loop to read all analog inputs
  for (uint8_t i = 0; i < N_sensors; i++) {   // loop through the sensors

    // get the temperature into our temp array
    temp[i] = readTemp(i);
    
    // call voortschrijdend gemiddelde function
    runAverage_1 ();
  }
}

int runAverage_1() {
  
  static int  LastMeas[RunAvgArr];      // LastMeasurements
  static byte index = 0;
  static long sum = 0;
  static int  count = 0;

  // keep sum updated to improve speed.
  sum            -= LastMeas[index];
  LastMeas[index] = temp[i];
  sum            += LastMeas[index];
  index++;
  index           = index % RunAvgArr;
  if (count < RunAvgArr) count++;

  return sum / count;
}


i hope i've put all the brackets in oke, if not that it an error in this sniblett and not in the full progam..

I would really appreciate some tips on how to get this to run correctly.
but i need to understand why and how. so if anybody feels like getting it finished.. oke with me but please explain in layman's terms..

personally i have the feeling that it will endup being soms sort of 2D array..
but i have no clue on how to implemnt that in this non typical situation..
did re-read in in to 2D array's but.. it made me get lost.

thanks in advance

Matt
Last edited on
Pseudo-code. Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdint>

int main() {

	using Byte = std::uint8_t;

	const Byte num_sensors = 2;//Number of sensors
	const Byte num_samples = 10;//Number of samples to read from each sensor

	Byte data[num_sensors][num_samples] = {};

	for (Byte current_sample = 0; current_sample < num_samples; ++current_sample) {

		for (Byte current_sensor = 0; current_sensor < num_sensors; ++current_sensor) {

			data[current_sensor][current_sample] = readDataFromSensor(current_sensor);

		}

	}

	/**/

	return 0;
}


Note that the data array in my example is an array of Bytes. You'll have to change the type depending on what sort of data you're capturing.
Last edited on
@ gentleguy..

compiler errors here are irrelevent. I'm trying to put in code in the right way. every different try gives a different error. but that is because the code is not correct typed. i'm looking for the implementation. a step before when compiler errors come into play.

here it would say that temp[i] the i hasn't been declared. ofcourse it isn't..

@xismn:

well i could be wrong but i believe not. what you post is a mere nested for loop.

in my case the runningaverage code is a loop itself. not a for loop but a loop. well that is what i believe it to be anyway.)

that is why i have it like i have it now. calling the function within the first for loop as the function is the 2e loop. but then i have problems with my array. how to implement them in the two different loops. should it be 2x 1D or a 2D loop. i have tried several wrong answers. just haven't found the right option yet. unless a right one was discarded as wrong. but then comes into play that i would like to know why.

i've also looked into this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int runAverage_1() {
  
  static int  LastMeas[N_sensors][RunAvgArr];      // LastMeasurements
  static byte index = 0;
  static long sum = 0;
  static int  count = 0;

  // keep sum updated to improve speed.
  sum                       -= LastMeas[N_sensors][index];
  LastMeas[N_sensors][index] = temp[i];
  sum                       += LastMeas[N_sensors][index];
  index++;
  index                      = index % RunAvgArr;
  if (count < RunAvgArr) count++;

  return sum / count;
}


ofcourse again a sniblett.
Topic archived. No new replies allowed.