Storing sensor value as previous and comparing it with current value

Hello,

I am using a pressure sensor and a solenoid for a project. I want to open/close the valve using the pressure difference between two values given by the pressure sensor. The pressure sensor is programmed to read the pressure values which gives an output like this:
123
132
113
50
231
251
.
.
.
I want to find out the difference between the current value and previous value(eg:251-231) and if the difference is greater than a certain number, the solenoid will be opened.
How do I find this value?

Where is pressure_mmhg declared and where does it get its value ?

Where does the loop() function end ?

Where is pressure_mmhg declared and where does it get its value ?

I declared Pressure_mmhg like this.
pressure = 1.0 * (bridge_data - OUTPUT_MIN) * (PRESSURE_MAX - PRESSURE_MIN) / (OUTPUT_MAX - OUTPUT_MIN) + PRESSURE_MIN;
pressure_mmhg = 0.750062 * pressure;

Sorry , I forgot to put it in the code in the above post.
I corrected the code in the first post.

Which code are we talking about ?

In the first code in your original post
Where is pressure_mmhg declared and where does it get its value ?

In the second code in your original post
Where is pressure_mmhg declared and where does it get its value ?
Where is previous_mmhg declared and where does it get its value ?

I corrected the code in the first post.

In future, please post revised code in a new comment to avoid confusion when comments have already been made about the original code that no longer make sense.

dpoornima:
Hello,

I am using a pressure sensor and a solenoid for a project. I want to open/close the valve using the pressure difference between two values given by the pressure sensor. The pressure sensor is programmed to read the pressure values which gives an output like this:
123
132
113
50
231
251
.
.
.
I want to find out the difference between the current value and previous value(eg:251-231) and if the difference is greater than a certain number, the solenoid will be opened.
How do I find this value?
The code for the pressure sensor is:

#include "Wire.h"

#include <Arduino.h>

#define HSCDRRN400MD2A3_I2C 0x28 
#define OUTPUT_MIN 1638.4       
#define OUTPUT_MAX 14745.6     
#define PRESSURE_MIN -400     
#define PRESSURE_MAX 400

void setup()
{
Wire.begin();
delay (500);
Serial.begin(9600);
}

void loop()
{
float pressure, temperature;

Wire.beginTransmission(HSCDRRN400MD2A3_I2C);
Wire.write(1); 
Wire.endTransmission();

delay (20);

Wire.requestFrom(HSCDRRN400MD2A3_I2C, 4);
while(Wire.available() == 0);
byte a    = Wire.read();
byte b    = Wire.read();
byte c    = Wire.read();
byte d    = Wire.read();
byte status1 = (a & 0xc0) >> 6;

int bridge_data = ((a & 0x3f) << 8) + b;
  int temperature_data = ((c << 8) + (d & 0xe0)) >> 5;
pressure = 1.0 * (bridge_data - OUTPUT_MIN) * (PRESSURE_MAX - PRESSURE_MIN) / (OUTPUT_MAX - OUTPUT_MIN) + PRESSURE_MIN;
pressure_mmhg = 0.750062 * pressure;
    temperature = (temperature_data * 0.0977) - 50;

Serial.println(pressure_mmhg);
 
         
          delay (500);
}





I tried to do it like this:



int solenoidPin = 4;
  int currentpressure_mmhg;
  int previouspressure_mmhg;
void setup(){
}
void loop(){
  previouspressure_mmhg = currentpressure_mmhg; // the value which was read the last time will be stored as the current pressure
  currentpressure_mmhg=pressure_mmhg;
  if(currentpressure_mmhg-previous_mmhg >= 50)
  {
  digitalWrite(4, HIGH);    //Switch Solenoid ON
  }

else
  {
  digitalWrite(4, LOW);    //Switch Solenoid OFF
 
}
}





The code doesn't compile. 
Can you please tell me the mistake and help me fix the errors?


Thank you

dpoornima:
Hello,

I am using a pressure sensor and a solenoid for a project. I want to open/close the valve using the pressure difference between two values given by the pressure sensor. The pressure sensor is programmed to read the pressure values which gives an output like this:
123
132
113
50
231
251
.
.
.
I want to find out the difference between the current value and previous value(eg:251-231) and if the difference is greater than a certain number, the solenoid will be opened.
How do I find this value?
The code for the pressure sensor is:

#include "Wire.h"

#include <Arduino.h>

#define HSCDRRN400MD2A3_I2C 0x28 
#define OUTPUT_MIN 1638.4       
#define OUTPUT_MAX 14745.6     
#define PRESSURE_MIN -400     
#define PRESSURE_MAX 400

void setup()
{
Wire.begin();
delay (500);
Serial.begin(9600);
}

void loop()
{
float pressure, temperature;

Wire.beginTransmission(HSCDRRN400MD2A3_I2C);
Wire.write(1); 
Wire.endTransmission();

delay (20);

Wire.requestFrom(HSCDRRN400MD2A3_I2C, 4);
while(Wire.available() == 0);
byte a    = Wire.read();
byte b    = Wire.read();
byte c    = Wire.read();
byte d    = Wire.read();
byte status1 = (a & 0xc0) >> 6;

int bridge_data = ((a & 0x3f) << 8) + b;
  int temperature_data = ((c << 8) + (d & 0xe0)) >> 5;
pressure = 1.0 * (bridge_data - OUTPUT_MIN) * (PRESSURE_MAX - PRESSURE_MIN) / (OUTPUT_MAX - OUTPUT_MIN) + PRESSURE_MIN;
pressure_mmhg = 0.750062 * pressure;
    temperature = (temperature_data * 0.0977) - 50;

Serial.println(pressure_mmhg);
 
         
          delay (500);
}





I tried to do it like this:



int solenoidPin = 4;
  int currentpressure_mmhg;
  int previouspressure_mmhg;
void setup(){
}
void loop(){
  previouspressure_mmhg = currentpressure_mmhg; // the value which was read the last time will be stored as the current pressure
  currentpressure_mmhg=pressure_mmhg;
  if(currentpressure_mmhg-previous_mmhg >= 50)
  {
  digitalWrite(4, HIGH);    //Switch Solenoid ON
  }

else
  {
  digitalWrite(4, LOW);    //Switch Solenoid OFF
 
}
}





The code doesn't compile. 
Can you please tell me the mistake and help me fix the errors?


Thank you

Given below Compiled Codes:
int solenoidPin = 4;
int currentpressure_mmhg;
int previouspressure_mmhg;
int pressure_mmhg;
int previous_mmhg;

void setup()
{

}

void loop()
{
previouspressure_mmhg = currentpressure_mmhg; // the value which was read the last time will be stored
//as the current pressure
currentpressure_mmhg=pressure_mmhg;

if(currentpressure_mmhg-previous_mmhg >= 50)
{
digitalWrite(4, HIGH); //Switch Solenoid ON
}

else
{
digitalWrite(4, LOW); //Switch Solenoid OFF

}

}

Even though the difference between the values is greater than 30, the valve doesn't open/close.

Is there any way to store the pressure reading in a variable and subtract the new pressure reading from the previous reading and compare it with a value?

It's closer, but you set them up as integers with unknown initial values and then use them as "float".

Paul

Hello ,

Sorry I could not understand it properly.

First, read your pressure

I calculate the pressure and write it to a sd card .

Calculating pressure:

pressure_mmhg = 0.750062 * pressure;[quote][/quote]

Then compare to the previous pressure

How do I compare the pressure to the previous value without creating a variable to store the previous value?
If this is my data,
(ms) pressure
1445 -0.41
1486 -3.71
1527 -5.86
1568 -19.32
1611 -33.01
1652 -50.91
1693 -75.81
1734 -97.79
1777 -100.67
1818 -88.91
1859 -79.75
1902 -72.84
1943 -69.68
1984 -4.58
2025 6.35
2066 24.35,

where do I store the -0.41 and then compare -3.71 to it?

I am really sorry am so confused now :disappointed_relieved:

float previousPressure = 0;
float currentPressure = 0;

This did not strike me .

I am really sorry that you had to explain it so many times.

Thank you very much for your patience :sweat_smile: