Why this nibp program can't control the circuit properly

#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
}

Hi @apriliaatiya, could you please add code tags?
And could you tell what nibp stands for and whst the program should do, what it does and how that is different?
Can't control is really really vague...

I want to make an Arduino program to detect systolic values ​​without diastolic, using the HX7010B module with a driver circuit for the motor and solenoid using NPN, is this program correct?

#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
}

Are you using Arduino UNO? What type of sensor you are using to sense blood pressure? have you tested you codes? If yes, what is the result?

If you wrote this entire program without any testing along the way, then no it is not correct. You need to actually test the code so YOU can determine if it is correct or not. If it does work properly, then yes, it is correct.

Yes, I'm using an Arduino Uno, the pressure sensor I'm using is an HX710B and I've calibrated it, but why doesn't the motor stop pumping when I try it?

Hi, @apriliaatiya

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

We need to see what and how you have connected the hardware.

What are you using as a power supply

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


that's the circuit I use, the power supply uses a li ion battery which is stepped down to 5v then the motor driver and solenoid use an npn transistor

1 Like

I have merged your cross-posts @apriliaatiya .

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

That picture is useless. A picture of a hand drawn circuit is fine.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.