Help with functions / array

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);
    }*_

Note: the code did not post correctly???

I've tried copying it again but get the same posting problem???

in the 'updateArray' subroutine the 3rd line actually reads

"sensorArray(i) = sensorArray(i-1);"

I used parenthesis here instead of brackets because when I use brackets the first part of the statement changes in the post??? I do use brackets in the code.

jimscott77:
Code:

for (int i = 9; i < 1; i--) {

Your loop never executes. You want "i > 1". You set i to 9 and then immediately exit the loop because 9 is not less than 1.

Use code tags to post code.

additional posting errors:

In each case where the post reads 'sensorArray' the code actually reads 'sensorArray(1)' ... again using parenthesis instead of brackets ... for some reason the post is dropping the '[1]'

John,

Thanks, I was thinking "until", not "while". Fuzzy thinking.

Also, didn't know about the code tags, thanks for that as well.

Jim

You can edit your post.

Before the code, type [code] and after the code type [/code].