error reading response from IF condition - Mapping voltage to pressure readings

Good Day
I recently purhcased pressure sensor that work with arduino as output volt of sensor is 0.5 - 4.5 V
Linke here:

I have mapped voltages to PSI but I am getting contatant PSI “103” on serial monitor whereas I am getting different and correct volatge outputs, I belive I have done some mistake in programming and need your guidance please.
Code is as below:

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display


const float referenceVolts = 5.0; // the default reference on a 5-volt board
const int psiPin = 0;         // psi is connected to analog pin 0
int psi = 5; 

void setup() 
{
   //LCD Script
  lcd.init();                      // initialize the lcd 
  lcd.backlight();

 
  Serial.begin(9600);
}

void loop()
{
  int val = analogRead(psiPin); // read the value from the sensor 
  float volts = (val / 1023.0) * referenceVolts; // calculate the ratio
  Serial.println(volts); // print the value in volts
//    if ((volts >= 0.4) && (volts<=0.47) )
//    psi = 0; 

// if ((volts > 0.47) && (volts<0.49) )
//   psi = 10; 
 
   if ((volts >= 0.4) && (volts<=0.47) )    psi = 0; 
if ((volts > 0.47) && (volts<=0.49) )    psi = 50; 
if ((volts > 0.49) && (volts=0.51) )    psi = 100; 

 if ((volts >0.5) && (volts<=0.6) ) psi=103;
if ((volts >0.6) && (volts<=0.7) ) psi=105;
if ((volts >0.7) && (volts<=0.8) ) psi=108;
if ((volts >0.8) && (volts<=0.9) ) psi=110;
if ((volts >0.9) && (volts<=1) ) psi=113;
if ((volts >1) && (volts<=1.1) ) psi=115;
if ((volts >1.1) && (volts<=1.2) ) psi=118;
if ((volts >1.2) && (volts<=1.3) ) psi=120;
if ((volts >1.3) && (volts<=1.4) ) psi=123;
if ((volts >1.4) && (volts<=1.5) ) psi=125;
if ((volts >1.5) && (volts<=1.6) ) psi=128;
if ((volts >1.6) && (volts<=1.7) ) psi=130;
if ((volts >1.7) && (volts<=1.8) ) psi=133;
if ((volts >1.8) && (volts<=1.9) ) psi=135;
if ((volts >1.9) && (volts<=2) ) psi=138;
if ((volts >2) && (volts<=2.1) ) psi=140;
if ((volts >2.1) && (volts<=2.2) ) psi=143;
if ((volts >2.2) && (volts<=2.3) ) psi=145;
if ((volts >2.3) && (volts<=2.4) ) psi=148;
if ((volts >2.4) && (volts<=2.5) ) psi=150;
if ((volts >2.5) && (volts<=2.6) ) psi=153;
if ((volts >2.6) && (volts<=2.7) ) psi=155;
if ((volts >2.7) && (volts<=2.8) ) psi=158;
if ((volts >2.8) && (volts<=2.9) ) psi=160;
if ((volts >2.9) && (volts<=3) ) psi=163;
if ((volts >3) && (volts<=3.1) ) psi=165;
if ((volts >3.1) && (volts<=3.2) ) psi=168;
if ((volts >3.2) && (volts<=3.3) ) psi=170;
if ((volts >3.3) && (volts<=3.4) ) psi=173;
if ((volts >3.4) && (volts<=3.5) ) psi=175;
if ((volts >3.5) && (volts<=3.6) ) psi=178;
if ((volts >3.6) && (volts<=3.7) ) psi=180;
if ((volts >3.7) && (volts<=3.8) ) psi=183;
if ((volts >3.8) && (volts<=3.9) ) psi=185;
if ((volts >3.9) && (volts<=4) ) psi=188;
if ((volts >4) && (volts<=4.1) ) psi=190;
if ((volts >4.1) && (volts<=4.2) ) psi=193;
if ((volts >4.2) && (volts<=4.3) ) psi=195;
if ((volts >4.3) && (volts<=4.4) ) psi=198;
if ((volts >4.4) && (volts<=4.5) ) psi=200;

   
   
   
   Serial.println(psi);
    lcd.setCursor(1,1);
      lcd.print (psi);
      delay(5000);
          lcd.setCursor(1,1);
    lcd.print ("   ");
        }

Your mistake: “(volts = 0.51)”
You want: “(volts <= 0.51)”

You were setting ‘volts’ to 0.51 which you then mapped to psi=103

You can map the voltage directly to pressure since the 0.5 to 4.5V range is linear.

 float psi = ((volts - 0.5) / 4.0) * 200.0;

Thanks, I will re-write code and will update

johnwasser:
Your mistake: “(volts = 0.51)”
You want: “(volts <= 0.51)”

You were setting ‘volts’ to 0.51 which you then mapped to psi=103

Hi, If I want to go by option 1, then which line to be corrected?

if ((volts > 0.49) && (volts=0.51) )    psi = 100;

I belive in && condition volts should not be 0.51 since it’s within if ( )

if ((volts > 0.49) && (volts=0.51) ) Don’t you want “==” instead of “=” ?

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