Problem with LED diod HIGH delay on Arduino UNO. Delay of LED diod is controled by a pressure sensor

Hello everyone. I really need your help. Im working on my bachelor thesis but the code isnt working. The problem is in the LED diod HIGH delay. Whenever I apply force and the pressure value changes it gives out only the biggest delay possible, even tho the force is small.

Here is the code. I do really appreciate your help! (Im desperate)

#include <HX711.h>

#define LED_PIN 13 // Define the pin for the LED indicator
//#define DELAY_TIME 1000 // Delay time in milliseconds after pressure is applied
#define PRESSURE_READING_TIME 1500 // Time in milliseconds to read pressure when force is applied
#define PRESSURE_CHANGE_THRESHOLD 100 // Minimum change in pressure value to start the reading timer

HX711 scale;  // Initializes library functions.
int calibration_factor = -100000; // Defines calibration factor we'll use for calibrating.
float pressure_range_kPa = 40.0; // Pressure sensor range in kPa
float max_sensor_value = 1023.0; // Maximum sensor value
int previousPressureValue = 0; // Previous pressure value for comparison
unsigned long timerStartTime = 0; // Start time of the reading timer
int maxPressureValue = 0; // Maximum pressure value detected during the reading period
bool sickMode = false; // Flag to indicate the person's health condition (healthy by default)
int LED_HIGH = 1000;
void setup() 
{
  Serial.begin(9600);   // Starts serial communication in 9600 baud rate.

  Serial.println("Initializing scale calibration.");  // Prints user commands.
  Serial.println("Please remove all pressure from sensor.");
  Serial.println("Apply known pressure levels to the sensor.");
  Serial.println("Press 'C' for tare");

  scale.begin(A1, A0);   // Initializes the scaling process.
                         // Used pins are A0 and A1.

  scale.set_scale();
  scale.tare();          // Resets the scale to 0.

  pinMode(LED_PIN, OUTPUT); // Set LED pin as output
}
 

void loop() 
{
  if(Serial.available()) {
    char temp = Serial.read(); // Reads user keyboard inputs
    
    if(temp == 'c' || temp == 'C') {
      scale.tare(); // Reset the scale to zero if 'C' key is pressed
      digitalWrite(LED_PIN, LOW); // Turn off LED
      // Wait for a while to stabilize after tare
      delay(500);
      return; // Exit loop if 'C' key is pressed
    } else if (temp == '0') {
      sickMode = false; // Set the person's mode to healthy
      Serial.println("Healthy");
    } else if (temp == '1') {
      sickMode = true; // Set the person's mode to sick
      Serial.println("Sick");
    }
  }

  scale.set_scale(calibration_factor);  // Adjusts the calibration factor.
 
  Serial.print("Reading: ");            // Prints pressure readings in pascals.
  float pressure_kPa = scale.get_units() * (pressure_range_kPa / max_sensor_value);
  float pressure_Pa = pressure_kPa * 1000; // Convert pressure to pascals
  int pressureValue = int(pressure_Pa); // Convert pressure to integer value
  Serial.print(pressureValue);
  Serial.println(" Pa");
  // Check if pressure value changes significantly
  if (abs(pressureValue - previousPressureValue) > PRESSURE_CHANGE_THRESHOLD) {
    timerStartTime = millis(); // Start the reading timer
    maxPressureValue = pressureValue; // Update max pressure value
  }

  // Check if the reading timer has expired
  if (millis() - timerStartTime > PRESSURE_READING_TIME) {
    // Turn on LED with the highest pressure value
    if (sickMode) {
      if (maxPressureValue >= 300 && maxPressureValue <= (2*pressure_range_kPa)/3) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 1 second
        delay(LED_HIGH*3);
      } else if (maxPressureValue > (2*pressure_range_kPa)/3 && maxPressureValue <= (5*pressure_range_kPa)/6) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 3 seconds
        delay(LED_HIGH*5);
      } else if (maxPressureValue > (5*pressure_range_kPa)/6) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 5 seconds
        delay(LED_HIGH*7);
      }
    } else {
      // Healthy mode
      if (maxPressureValue >= 300 && maxPressureValue <= (2*pressure_range_kPa)/3) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 1 second
        delay(LED_HIGH);
      } else if (maxPressureValue > (2*pressure_range_kPa)/3 && maxPressureValue <= (5*pressure_range_kPa)/6) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 3 seconds
        delay(LED_HIGH*3);
      } else if (maxPressureValue > (5*pressure_range_kPa)/6) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 5 seconds
        delay(LED_HIGH*5);
      }
    }
    digitalWrite(LED_PIN, LOW); // Turn off LED
  }

  // Update previous pressure value
  previousPressureValue = pressureValue;

  // Wait for a while before taking the next reading
  delay(300);
}

and here is data from Serial Monitor:
Reading: 0 Pa

Reading: 0 Pa

Reading: 1 Pa

Reading: 6 Pa

Reading: 1696 Pa

Reading: 3230 Pa

Reading: 3134 Pa

Reading: 3279 Pa

Reading: 3614 Pa

Reading: 4059 Pa

Reading: 4283 Pa

Your problem description does not indicate a problem with the IDE. Hence your topic has been moved to a more suitable location on then forum.

 if (maxPressureValue >= 300 && maxPressureValue <= (2*pressure_range_kPa)/3) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 1 second
        delay(LED_HIGH);
      } else if (maxPressureValue > (2*pressure_range_kPa)/3 && maxPressureValue <= (5*pressure_range_kPa)/6) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 3 seconds
        delay(LED_HIGH*3);
      } else if (maxPressureValue > (5*pressure_range_kPa)/6) {
        digitalWrite(LED_PIN, HIGH); // Turn on LED for 5 seconds
        delay(LED_HIGH*5);
      }

You are measuring the pressure in pascals, but you are doing a comparison with a limit that is in kilopascals.

Be consistent with your units of measurement.

4283 > 5*40/6

Well, now i changed the units from kPa to Pa and it olny gives me the lowest possible delay even tho i apply biggest force possible

Then you need to dream up some new limits.

Will try, thank you

Whatever change I make with the limits it only shows the lowest pressure. Do you please have any suggestions for the limits?

For what weight, (kg/lb), is the load cell you are using?
Please, post a schematic wiring of your project.

The pressure sensor is ranged from 0-40kPa but im using a small balloon so the max pressure is usually around 4000 Pa. I dont have a wiring schematic but here is a schematic of the pressure sensor. My additional wiring is one LED diode connected to pin 13 and GND

5-HX710B-Module-Schematic-TK(1)

I would have thought that you were in the best position to determine the limits.
After all you are the one who knows what your project is meant to do.

We only know what you have told us about it.

Is the device for measuring something to do with respiration?

If your maximum reading is around 4000Pa, why not try limits of say 2kPa and 3kPa until you have the LED coming on for each of the 3 possible times.

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