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