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