Hello All!
I have this code for reading a pressure sensor and have tried to adjust the code so I can calibrate the pressure sensor reading but everything I try doesn't work. I get weird result when changing and it will quit reading uneven numbers etc. The sensor I am using is a MPX5100.
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int rawValue; // A/D readings
int offset = 40.5; // zero pressure adjust (~30-72)
int fullScale = 819.0; // 819 max pressure adjust (~798-840)
float pressure; // final pressure in mmHg
int Battery; // battery % of use
int pressurezero;
const int ZeroButton = 4; //put the zero button on pin 4. Connects this pin to ground when pressed.
const int PressurePin = A0; //pressure sensor is connected to this analog pin
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16,2);
pinMode(ZeroButton, INPUT_PULLUP);
}
void loop()
{
rawValue = analogRead(A0);
pressure = (rawValue - offset)* 655.00 / (fullScale - offset); // for calibrated pressure conversion
Battery = analogRead(A1)/8.05; // Display battery percentage at max 100%
{
if(digitalRead(ZeroButton) == LOW)
{
//zero button is currently pressed
pressurezero = analogRead(PressurePin);
}
rawValue = analogRead(PressurePin);
}
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0 , 0);
lcd.print("FLOW RATE:");
lcd.print(pressure,0);
lcd.print(" ");
if (Battery >65)
{
lcd.setCursor(0 , 1);
lcd.print("BATTERY:");
lcd.print(Battery);
lcd.print("% ");
}
else
{
lcd.setCursor(0 , 1);
lcd.print("*CHARGE BATTERY*");
}
delay(500);
}
Your post doesn't make much sense.
Please take a moment to clearly explain what the program is supposed to do and what it does instead.
I suggest to get it working properly with the serial monitor before adding the LCD display.
By the way, do you understand the difference between integers and floating point numbers?
int offset = 40.5;
The code does work and works with the lcd. The post makes sense to me.
The code reads the pressure sensor at mmhg, that is millimeters of mercury. That is the same as the reading on a blood pressure guage but I am not reading blood pressure just pressure something else. I can adjust my zero with the offset but can not adjust or calibrate my reading. If the reading is 295mmhg and is supposed to be 300 I want to be able to adjust the code or calculation to do so.
Yes! Why do you ask about the int & float? As you can see I am a newbie and learning.
Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.
If the code works, what is the problem?
A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.
Correct. Can you then explain why you use a float value with a fraction, to initialize in an integer variable in the following line?
int offset = 40.5;
KevinRoach:
The code does work and works with the lcd. The post makes sense to me.
It doesn't to me. But that's probably because I have no clue of what you're trying to do, of what you've done over the last weeks, etc. Please remember we know NOTHING about your project except what you tell us.
About your code: please fix the formatting - do a ctrl-T in the IDE. Makes it a lot more readable. It appears there are some unnecessary blocks in there but without indentation it's really hard to see.
The code reads the pressure sensor at mmhg, that is millimeters of mercury. That is the same as the reading on a blood pressure guage but I am not reading blood pressure just pressure something else. I can adjust my zero with the offset but can not adjust or calibrate my reading. If the reading is 295mmhg and is supposed to be 300 I want to be able to adjust the code or calculation to do so.
You'll have to look for linear regression, as you're trying to change the slope of your calculation. Assuming a linear response, it follows the formula y = a*x + b
where y is the calculated pressure, x is the analog input reading, a is the slope and b is the offset.
So to calibrate take at least two readings of a precisely known pressure, get the x values for these readings, and out of that calculate a and b.
Better yet: take a number of readings over the full scale, and plot them in a graph. This will confirm you whether you are indeed linear, or how much you'll be off using a linear approximation if not. Then use the least squares method (built in in LibreOffice Calc, I suppose also in other spreadsheets like Excel) to calculate the slope and offset for that line.
jremington:
If the code works, what is the problem?
Correct. Can you then explain why you use a float value with a fraction, to initialize in an integer variable in the following line?
int offset = 40.5;
Because I made a mistake in typing it in that way! I'm bad
jremington:
If the code works, what is the problem?
Correct. Can you then explain why you use a float value with a fraction, to initialize in an integer variable in the following line?
int offset = 40.5;
Read what I originally posted!
I want to calibrate my pressure reading. If it is reading 295 and is supposed to be 300mmhg I want to be able to make it read what it truly is supposed to be.
wvmarle:
It doesn't to me. But that's probably because I have no clue of what you're trying to do, of what you've done over the last weeks, etc. Please remember we know NOTHING about your project except what you tell us.
About your code: please fix the formatting - do a ctrl-T in the IDE. Makes it a lot more readable. It appears there are some unnecessary blocks in there but without indentation it's really hard to see.
You'll have to look for linear regression, as you're trying to change the slope of your calculation. Assuming a linear response, it follows the formula y = a*x + b
where y is the calculated pressure, x is the analog input reading, a is the slope and b is the offset.
So to calibrate take at least two readings of a precisely known pressure, get the x values for these readings, and out of that calculate a and b.
Better yet: take a number of readings over the full scale, and plot them in a graph. This will confirm you whether you are indeed linear, or how much you'll be off using a linear approximation if not. Then use the least squares method (built in in LibreOffice Calc, I suppose also in other spreadsheets like Excel) to calculate the slope and offset for that line.
The code and pressures are linear, done created a slope to make sure. I have figured out another way to do what I want with messing with that code.
Thanks for the help!