#include <Arduino.h>
// Constants for ADC pin connections
const int pinBreathingFrequency = 34;
const int pinTidalVolume = 35;
const int pinPulmonaryPressure = 32;
const int pinVitalCapacity = 33;
const int pinPEEP = 25;
const int pinRespiratoryRate = 26;
// Constants for simulation ranges and thresholds
const int minBreathingFrequency = 8;
const int maxBreathingFrequency = 40;
const float minTidalVolume = 400.0; // in mL
const float maxTidalVolume = 600.0; // in mL
const int minPulmonaryPressure = 2; // in mmHg
const int maxPulmonaryPressure = 55; // in mmHg
const float minVitalCapacity = 2.5; // in Liters
const float maxVitalCapacity = 4.5; // in Liters
const int minPEEP = 3; // in cm H2O
const int maxPEEP = 8; // in cm H2O
const int minRespiratoryRate = 12;
const int maxRespiratoryRate = 41;
// Oxygen adjustment thresholds
const float lowCriticalTidalVolume = 450.0; // Lower Tidal volume threshold in mL
const float highCriticalTidalVolume = 550.0; // Higher Tidal volume threshold in mL
const int lowCriticalRespiratoryRate = 20; // Lower Respiratory rate threshold
const int highCriticalRespiratoryRate = 30; // Higher Respiratory rate threshold
void setup() {
Serial.begin(115200);
Serial.println("ESP32 Auto-weaning Ventilator Started");
analogReadResolution(12); // Set ADC resolution to 12-bit
}
int readPotValue(int pin, int minValue, int maxValue) {
int sensorValue = analogRead(pin);
return map(sensorValue, 0, 4095, minValue, maxValue);
}
void adjustOxygen(float tidalVolume, int respiratoryRate) {
if (tidalVolume < lowCriticalTidalVolume || respiratoryRate > highCriticalRespiratoryRate) {
Serial.println("Increase Oxygen Delivery!");
} else if (tidalVolume > highCriticalTidalVolume || respiratoryRate < lowCriticalRespiratoryRate) {
Serial.println("Decrease Oxygen Delivery!");
} else {
Serial.println("Oxygen Delivery Normal.");
}
}
void loop() {
int breathingFrequency = readPotValue(pinBreathingFrequency, minBreathingFrequency, maxBreathingFrequency);
float tidalVolume = readPotValue(pinTidalVolume, minTidalVolume * 10, maxTidalVolume * 10) / 10.0;
int pulmonaryPressure = readPotValue(pinPulmonaryPressure, minPulmonaryPressure, maxPulmonaryPressure);
float vitalCapacity = readPotValue(pinVitalCapacity, minVitalCapacity * 10, maxVitalCapacity * 10) / 10.0;
int PEEP = readPotValue(pinPEEP, minPEEP, maxPEEP);
int respiratoryRate = readPotValue(pinRespiratoryRate, minRespiratoryRate, maxRespiratoryRate);
adjustOxygen(tidalVolume, respiratoryRate);
Serial.print("Breathing Frequency: "); Serial.println(breathingFrequency);
Serial.print("Tidal Volume: "); Serial.println(tidalVolume);
Serial.print("Pulmonary Pressure: "); Serial.println(pulmonaryPressure);
Serial.print("Vital Capacity: "); Serial.println(vitalCapacity);
Serial.print("PEEP: "); Serial.println(PEEP);
Serial.print("Respiratory Rate: "); Serial.println(respiratoryRate);
Serial.println("---------------------------------------------");
delay(1000); // Update every second
}