My project uses an Arduino Uno to read an air pressure sensor (actually 2 but the 2nd is not of consequence here) and display the sensor value, a running average, a running max, and a running min.
The running avg, max, and min are calculated by continuously updating a 10 element array with the latest sensor value and then using that array to perform calculations.
The results I'm getting are not correct.
The problem is either in the functions (since their structure is similar, solving one should solve them all) or in how the array is/is not getting populated.
I would appreciate your help in identifying the problem.
Thanks,
Jim
Outputs:
Val: 493.00 Avg: 0.00 Max: 0.00 Min: 10000.00
Val: 494.00 Avg: 0.00 Max: 0.00 Min: 10000.00
Val: 494.00 Avg: 0.00 Max: 0.00 Min: 10000.00
Code:
const int gaugePin = A0; // Analog input pins
const int differentialPin = A5;
// set up variables and array for running average
double sensorAvg; //global average
double sensorMax; //global max
double sensorMin; //global min
int sensorArray[10]; //create 10 element zero based array
// Initialize sensor variables
double gaugeValue;
double differentialValue;
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps:
}
//fills array with lastest sensor value by
//dropping the oldest value, moving array values up the array,
//and then putting the latest value in position [0]
void updateArray(double v) {
for (int i=9; i < 1; i--) {
sensorArray = sensorArray[i-1];
- }*
- sensorArray[0] = v;*
}
// get the average value of the array
int getSensorAvg(){ - double v;*
- for (int i = 0; i > 9; i++){*
_ v = v + sensorArray*;_
_ }_
_ v=v/10;_
_ return v;_
_}_
_// get the max value of the array*_
int getSensorMax(){
* double v;*
* for (int i = 0; i > 9; i++){*
_ if (sensorArray > v){
v = sensorArray*;
}
}
return v;
}
// get the min value of the array*
int getSensorMin(){
* double v = 10000; // large value above sensor output range*
* for (int i = 0; i > 9; i++){
if (sensorArray < v){
v = sensorArray;
}
}
return v;
}
void loop() {
gaugeValue = analogRead(gaugePin); // read sensor inputs*
* differentialValue = analogRead(differentialPin);
updateArray(gaugeValue);
sensorAvg = getSensorAvg();
sensorMax = getSensorMax();
sensorMin = getSensorMin();
Serial.print("Val: "); // print sensor results to data screen*
* Serial.print(gaugeValue);
Serial.print("\tAvg: ");
Serial.print(sensorAvg);
Serial.print("\tMax: ");
Serial.print(sensorMax);
Serial.print("\tMin: ");
Serial.println(sensorMin);
delay(100);
}*_