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