Average Pressure Sensor Readings to Boyles Law

I am currently using a Spark Fun Qwiic MicroPressure sensor for a project in order to determine internal fluid volume with pressure. I do not have a strong background with Arduino. I am currently using the following code that I downloaded from a library involving the pressure sensor. This code is found below:

// Include the SparkFun MicroPressure library.
// Click here to get the library: http://librarymanager/All#SparkFun_MicroPressure

#include<Wire.h>
#include <SparkFun_MicroPressure.h>

/*
 * Initialize Constructor
 * Optional parameters:
 *  - EOC_PIN: End Of Conversion (defualt: -1)
 *  - RST_PIN: Reset (defualt: -1)
 *  - MIN_PSI: Minimum Pressure (default: 0 PSI)
 *  - MAX_PSI: Maximum Pressure (default: 25 PSI)
 */
//SparkFun_MicroPressure mpr(EOC_PIN, RST_PIN, MIN_PSI, MAX_PSI);
SparkFun_MicroPressure mpr; // Use default values with reset and EOC pins unused

void setup() {
  // Initalize UART, I2C bus, and connect to the micropressure sensor
  Serial.begin(115200);
  Wire.begin();

  /* The micropressure sensor uses default settings with the address 0x18 using Wire.

     The mircropressure sensor has a fixed I2C address, if another address is used it
     can be defined here. If you need to use two micropressure sensors, and your
     microcontroller has multiple I2C buses, these parameters can be changed here.

     E.g. mpr.begin(ADDRESS, Wire1)

     Will return true on success or false on failure to communicate. */

  if(!mpr.begin())
  {
    Serial.println("Cannot connect to MicroPressure sensor.");
    while(1);
  }
}

void loop() {
 
  Serial.print(mpr.readPressure(),4);
  Serial.println(" PSI");

  delay(500);
}

I would like to be able to take an average of a finite number of readings, and plug back in to a Boyle's Law equation (P1V1 = P2V2) with P1 and V1being already defined constants (Average pressure would be P2, solving for V2). Any help would be greatly appreciated.

int sum = 0;
for (int i=0; i<16; i++) {
   sum = sum+mpr.readPressure();
}
avg = sum/16;

Since the return value of readPressure() is a float variable, the code posted in #2 will give inaccurate results.

Declare sum to be float or double instead of int.

Can you provide an example of what you mean? I did receive an error on the replied code

How did you put the suggested code in your sketch?
What is the error message?
You need to add

float avg;
float sum=0.0;

Before the code and remove

int sum=0;

I placed the suggested code at the end of the current code I posted in the original post

Current error that I am experiencing with the:
float avg;
float sum+0.0;

Error message is:
" avg = sum/16;
^~~

exit status 1

Compilation error: expected initializer before numeric constant"

Please post the revised code (all of it) in a new post, using code tags.

Always post the complete text of error messages, also using code tags.

This won't work, please pay very careful attention to formatting and symbols.

float sum+0.0;

Looking to use a the average sensor reading value and place into an equation that prints out a desired value. The current code I am working with is the following:

// Include the SparkFun MicroPressure library.
// Click here to get the library: http://librarymanager/All#SparkFun_MicroPressure

#include<Wire.h>
#include <SparkFun_MicroPressure.h>



//SparkFun_MicroPressure mpr(EOC_PIN, RST_PIN, MIN_PSI, MAX_PSI);
SparkFun_MicroPressure mpr; // Use default values with reset and EOC pins unused

void setup() {
  // Initalize UART, I2C bus, and connect to the micropressure sensor
  Serial.begin(115200);
  Wire.begin();

  
  if(!mpr.begin())
  {
    Serial.println("Cannot connect to MicroPressure sensor.");
    while(1);
  }
}


void loop() {
 
  /* Serial.print(mpr.readPressure(),4);
  Serial.println(" PSI");

  Serial.println();
  delay(1500);
  */
  float avg;
float sum=0.0;
for (int i=0; i<16; i++)
 {
   sum = sum+mpr.readPressure();
}
avg = sum/16;
}

The equation I would like to plug into is the following:

V2 = P1 - ((P1*V1)/P2)

V1 and P1 are defined constant values that I would like to plug into code manually
P2 is the average reading of the pressure sensor in my current code
V2 is a value I would like to be able to print out once found, preferably in mL
P1 and P2 must be defined in psi

Any and all help is welcomed

You have the formula, so what is difficult?
What is your best try?
You are supposed to do some work, we are supposed to help if it does not compile or work as expected...
And you are not supposed to remove earlier posts or start a new thread on the same subject.

Please do not cross post.

Noted

Hi, @justintheman67
Welcome to the forum.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Which fluid?

Hi,

Boyle's Law is a gas law, not a fluid law.

Can you explain your project and process you aim to use?

Tom... :smiley: :+1: :coffee: :australia:

Just checking if "fluid" was gas or liquid. :roll_eyes:

Hi,
Yes, it sounds like @justintheman67 wants to measure the gas properties above a liquid in a sealed vessel, to calculate the fluid volume change under the volume of gas.

Tom.. :smiley: :+1: :coffee: :australia:
@justintheman67 PS. Nice and interesting project. :+1:

Hi All,

Quick update on my project. I have gained the ability to print the volume of the internal fluid in mL of my container. The air input is driven by 2 interconnected pneumatic cylinders with a manual switch regulating air flow to each side of the driving cylinder. Unfortunately, the pneumatic cylinders are not connected with my pressure sensor. I was wondering if there is a way to "start" the code, or to initiate printing of the code at a specific value of my "avg" variable (which is the pressure input of the pneumatic cylinders). I have attached my current code below, thank you all for your help so far.

// Include the SparkFun MicroPressure library.
// Click here to get the library: http://librarymanager/All#SparkFun_MicroPressure

#include<Wire.h>
#include <SparkFun_MicroPressure.h>



//SparkFun_MicroPressure mpr(EOC_PIN, RST_PIN, MIN_PSI, MAX_PSI);
SparkFun_MicroPressure mpr; // Use default values with reset and EOC pins unused

void setup() {
  // Initalize UART, I2C bus, and connect to the micropressure sensor
  Serial.begin(115200);
  Wire.begin();

  
  if(!mpr.begin())
  {
    Serial.println("Cannot connect to MicroPressure sensor.");
    while(1);
  }
}


void loop() {
 
  /* Serial.print(mpr.readPressure(),4);
  Serial.println(" PSI");

  Serial.println();
  delay(1500);
  */
  float avg;
float sum=0.0;
for (int i=0; i<4; i++)
 {
   sum = sum+mpr.readPressure();
}
avg = sum/4;

float i = 4000.00;
float v = 4205.98;
float m = 15.0494;
float atm = 14.2867;
float n = avg;
float c = 0;


float volume = c+ (i-((v*(m-atm))/(n-atm)));
Serial.print(avg, 4);
Serial.println("PSI New Input");
Serial.print(volume, 4);
Serial.println("mL");
delay(1000);
}

Best is to put the read avg pressure in a function an then in loop add:

const float limit = // set limit here
while (getAvgPressure() < limit) {
   ;  // do nothing
}