Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.7 kB
0
Indexable
Never
#include <Wire.h>
#include <Adafruit_MPRLS.h>

#define TCAADDR 0x70 // Address of the TCA9548A multiplexer

Adafruit_MPRLS mpr1 = Adafruit_MPRLS(-1, -1); // Sensor 1
Adafruit_MPRLS mpr2 = Adafruit MPRLS(-1, -1); // Sensor 2

const int airflowPin = A0; // Analog pin for airflow sensor
const float sampleArea = 25.0; // 25 cm² in square centimeters
const float sampleThickness = 0.0025; // 2.5 mm in meters (assuming 2.5 mm thickness)

void tcaSelect(uint8_t i) {
  if (i > 7) return;
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
}

void setup() {
  Serial.begin(9600);
  Wire.begin();

  // Initialize sensor 1
  tcaSelect(0);
  if (!mpr1.begin()) {
    Serial.println("Failed to find MPRLS sensor 1!");
    while (1) {}
  }

  // Initialize sensor 2
  tcaSelect(1);
  if (!mpr2.begin()) {
    Serial.println("Failed to find MPRLS sensor 2!");
    while (1) {}
  }
}

void loop() {
  // Read from pressure sensor 1
  tcaSelect(0);
  float pressure1_hPa = mpr1.readPressure();
  float pressure1_Pa = pressure1_hPa * 100; // Convert to Pascals

  // Read from pressure sensor 2
  tcaSelect(1);
  float pressure2_hPa = mpr2.readPressure();
  float pressure2_Pa = pressure2_hPa * 100; // Convert to Pascals

  // Calculate the pressure drop
  float deltaP = pressure1_Pa - pressure2_Pa;

  // Read from airflow sensor
  int airflowValue = analogRead(airflowPin);
  float voltage = airflowValue * (5.0 / 1023.0);
  float airflow = (voltage - 1.0) * 20.0 / 60.0; // Convert to dm³/min (Example conversion, adjust based on calibration)

  // Calculate airflow resistance
  float airflowResistance = deltaP / (airflow * 1.66667e-5); // Convert dm³/min to m³/s

  // Calculate specific airflow resistance
  float specificAirflowResistance = airflowResistance / sampleArea; // in Pa·s/cm²

  // Calculate airflow resistivity
  float airflowResistivity = specificAirflowResistance / sampleThickness; // in Pa·s/m

  // Print the results
  Serial.print("Pressure 1: ");
  Serial.print(pressure1_Pa);
  Serial.println(" Pa");

  Serial.print("Pressure 2: ");
  Serial.print(pressure2_Pa);
  Serial.println(" Pa");

  Serial.print("Airflow: ");
  Serial.print(airflow);
  Serial.println(" dm³/min");

  Serial.print("Airflow Resistance: ");
  Serial.print(airflowResistance);
  Serial.println(" Pa·s/m³");

  Serial.print("Specific Airflow Resistance: ");
  Serial.print(specificAirflowResistance);
  Serial.println(" Pa·s/cm²");

  Serial.print("Airflow Resistivity: ");
  Serial.print(airflowResistivity);
  Serial.println(" Pa·s/m");

  delay(1000); // Wait for 1 second before the next reading
}
Leave a Comment