AVERAGE OF CURRENT VALUE + PREVIOUS VALUE

Hello to our community of contributors here in Arduino ;D

Can somebody help us for our code? We want to get the average of the previous pressure value and the current pressure value, with the interval being 10seconds.

For example:

Value@Start/0 seconds: 1

then after 10 seconds,

Value@10seconds: 2
Average of Previous 2 Values = (1+2)/2

Thanks for your help! :slight_smile:

const float Offset = 0.483 ;

float V, P;

void setup()
{ Serial.begin(9600);
  Serial.println("/** Water Pressure Sensor demo **/");
}

void loop() {
  V = analogRead(0) * 5.00 / 1024;
  P = (V - Offset) * 400;

  Serial.print("Voltage:");
  Serial.print(V, 3);
  Serial.println("V");

  Serial.print("Pressure");
  Serial.print(P, 1);
  Serial.println(" KPa");
  Serial.println();

  delay(1000);
}

Before you read the current pressure value save the previous value to a variable.

After you read the current value do the calculation

averageValue = (previousValue + currentValue) / 2;

If the code is not required to do anything else then use delay(10000) to achieve the 10 second interval otherwise you will need to use millis() for timing as delay() stops the program doing anything else during the delay period.

Ideally, where would I put it in the code that I pasted? Does it matter if it comes first or not? Thank you!

Can you give me a short example with the actual code?

where would I put it in the code that I pasted?

Before you read the current pressure value save the previous value to a variable.

Thank you, will try that. :slight_smile: