#include "HX710.h"
#define HX710B_DATA_PIN 5 // Data pin from HX710B
#define HX710B_CLOCK_PIN 18 // Clock pin from HX710B
#define MOTOR_PIN 2 // Pin for the inflation motor
#define VALVE_PIN 15 // Pin for the main valve (gradual deflation)
long offset = 0; // Offset value when pressure = 0 kPa
long previousRawValue = 0;
long rawValue = 0;
float pressure = 0.0;
const float pressureMax = 33.33; // Maximum pressure in kPa (250 mmHg pressure)
long digitalMin = 46500; // Digital value corresponding to 0 kPa (requires calibration)
long digitalMax = 2880000; // Digital value corresponding to 40 kPa (requires calibration)
const int kPa_to_mmHg = 7.50062;
const int inflasiOscillationMinThreshold = 17000;
const int inflasiOscillationMaxThreshold = 20000;
const int additionalPressure_mmHg = 30;
const int deflationThresholdHigh = 15000; // Systolic oscillation threshold
const int deflationStep_mmHg = 2; // Pressure decrease per step in mmHg
const int deflationDelay = 100; // Stabilization delay after deflation (ms)
unsigned long lastDeflationTime = 0;
// Status variables
bool firstReading = true;
bool oscillationDetected = false;
bool deflationStarted = false;
bool deflationActive = false;
bool systolicDetected = false;
int initialPressure_mmHg = 0;
int systolicPressure = 0;
int targetPressure_mmHg = 0;
// Manual feature
const int defaultInflationPressure = 160; // Default inflation pressure
int userDefinedMaxPressure = 200; // Manual inflation pressure (can be adjusted as needed)
HX710 ps;
void setup() {
Serial.begin(115200); // Start serial communication
pinMode(HX710B_DATA_PIN, INPUT);
pinMode(HX710B_CLOCK_PIN, OUTPUT);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(VALVE_PIN, OUTPUT);
digitalWrite(MOTOR_PIN, LOW); // Turn off the inflation motor (HIGH=ON)
digitalWrite(VALVE_PIN, HIGH); // Close the deflation valve (LOW=OFF)
// Perform initial calibration (with 0 kPa pressure)
offset = readHX710B(); // Offset value when pressure = 0 kPa
digitalMin = offset; // Calibrate the digital value for 0 kPa
Serial.println("Setup started");
ps.initialize(HX710B_CLOCK_PIN, HX710B_DATA_PIN);
// Inform about manual inflation pressure
Serial.print("Manual Inflation Pressure Set To: ");
Serial.print(userDefinedMaxPressure);
Serial.println(" mmHg");
}
void loop() {
// Read raw value from HX710B
rawValue = readHX710B();
// Calculate pressure based on raw value
pressure = (((float)(rawValue - digitalMin) / (digitalMax - digitalMin)) * pressureMax) - 0.54;
int mmhg = (pressure * kPa_to_mmHg);
if (!deflationActive) {
// Inflation stage
if (!deflationStarted) {
if (firstReading) {
previousRawValue = rawValue;
firstReading = false;
}
// Detect initial oscillations
if (!oscillationDetected && mmhg >= 60 &&
abs(rawValue - previousRawValue) >= inflasiOscillationMinThreshold &&
abs(rawValue - previousRawValue) <= inflasiOscillationMaxThreshold) {
oscillationDetected = true;
initialPressure_mmHg = mmhg;
}
// Stop inflation if manual pressure or oscillation-based pressure is reached
if (mmhg >= userDefinedMaxPressure ||
(oscillationDetected && mmhg >= initialPressure_mmHg + additionalPressure_mmHg)) {
stopMotor();
deflationStarted = true;
deflationActive = true;
targetPressure_mmHg = mmhg - deflationStep_mmHg; // Set initial target pressure for deflation
}
}
} else {
// Gradual deflation stage
if (mmhg <= targetPressure_mmHg) {
digitalWrite(VALVE_PIN, LOW); // Close the main valve
targetPressure_mmHg -= deflationStep_mmHg;
if (millis() - lastDeflationTime >= deflationDelay) {
lastDeflationTime = millis();
// Detect systole
int oscillationAmplitude = abs(rawValue - previousRawValue);
if (!systolicDetected && oscillationAmplitude >= deflationThresholdHigh) {
systolicDetected = true;
systolicPressure = mmhg;
Serial.print("Systolic Pressure: ");
Serial.println(systolicPressure);
// Open the valve fully to release pressure
digitalWrite(VALVE_PIN, HIGH);
deflationActive = false;
}
}
} else {
digitalWrite(VALVE_PIN, HIGH); // Open valve for gradual deflation
}
}
// Display monitoring data
Serial.print("Raw Value: ");
Serial.print(rawValue);
Serial.print(" Pressure: ");
Serial.print(mmhg);
Serial.println(" mmHg");
previousRawValue = rawValue;
delay(50);
}
long readHX710B() {
long value = 0;
unsigned long startTime = millis();
while (digitalRead(HX710B_DATA_PIN) == HIGH) {
if (millis() - startTime > 1000) { // Timeout after 1 second
Serial.println("HX710 timeout");
return -1;
}
}
for (int i = 0; i < 24; i++) {
digitalWrite(HX710B_CLOCK_PIN, HIGH);
value = value << 1;
digitalWrite(HX710B_CLOCK_PIN, LOW);
if (digitalRead(HX710B_DATA_PIN)) {
value++;
}
}
digitalWrite(HX710B_CLOCK_PIN, HIGH);
digitalWrite(HX710B_CLOCK_PIN, LOW);
if (value & 0x800000) {
value |= ~0xFFFFFF;
}
return value;
}
void stopMotor() {
digitalWrite(MOTOR_PIN, LOW); // Turn off the inflation motor
}
