oil pressure sensor code

I am trying to read a oil pressure sensor

this is the code i have found online to do so but its incomplete.

OilPressure = (float(OilPressure1(analogRead(2))));

float OilPressure1(float RawADC)
{
float Pressure;
Pressure = (RawADC/1024.00)5; //calculate actual voltage across the sensor
Pressure = (-.177 + sqrt(.03133-(4
-.0065*(.052-Pressure))))/-.013; //quadratic formula to get a more acurate reading based on the curve
Pressure = Pressure * 14.7; //convert from bar to PSI
return Pressure;
}

Im extremely new to the Arduino, and coding in general.
I have tried dozens of times to get it to compile, but to no avail.
So any help I can get to finish the code or point me in the right direction that would be awesome!

Can you provide a link to the datasheet of the sensor?

I can compile your code (after some fixes) but there is a math error in the formula => sqrt(negative number)

so please post the link to the datasheet.

Thank you very much!

according to Excel (fill in table -> spread graph -> trend line)
the formula is completeley different.

bar = ((2.1271 * volt) + 5.1075 ) * volt - 0.2561; // R2 = 1 !
psi = bar * 14.7;

uint32_t start;
uint32_t stop;

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");

  start = micros();
  float OilPressure = GetOilPressure(analogRead(2));
  stop = micros();
  Serial.print("time:\t");
  Serial.println(stop-start);
  Serial.print("OilPressure:\t");
  Serial.println(OilPressure, 3);
}

void loop() 
{
}

float GetOilPressure(int RawADC)
{
  float volt = RawADC * (5.0/1023.0);
  float bar = ((2.1271 * volt) + 5.1075 ) * volt - 0.2561; // optimized
  float psi = bar * 14.7; 
  return psi;
}

as the voltage probably is always below 1.0 volt you might use analogReference() to set the internal 1.1 volt reference => getting more accurate readings. Note you have to patch the voltage formula.

It looks like the sensor is being used as a voltage divider with a 990 Ohm resistor to +5V.

Since all of the results are under 0.8V You can get more precision by using the internal 1.1V analog reference instead of the default 5V reference.

You can convert from the "Voltage" number to Analog Input number by multiplying by 1024/1.1 and then use
MultiMap to do a piecewise liner interpolation:

http://playground.arduino.cc/Main/MultiMap

Please correct me if Im wrong but would this be correct, anything wrong or should be changed?

As for the sensor wiring still connect +5v to voltage dividersensor?

int OilPressure = 0;
#define aref_voltage 1.1

void setup() 
{
 Serial.begin(9600);
 analogReference(INTERNAL);
 
}

void loop() 
{
 
 
 float OilPressure = GetOilPressure(analogRead(2));
 Serial.print("OilPressure: ");
 Serial.println(OilPressure, 0);
 delay(1000);
}

float GetOilPressure(int RawADC)
{
 float volt = RawADC * (aref_voltage/1023.0);
 float bar = ((2.1271 * volt) + 5.1075 ) * volt - 0.2561; // optimized
 float psi = bar * 14.7; 
 return psi;
}

moderator: added code tags [ code] [ /code]

Looks correct to me. Does it produce reasonable answers?

Ok Im having trouble implementing this code into my final sketch.
any help is appreciated.
I need this.

int OilPressure = 0;
#define aref_voltage 1.1

void setup() 
{
 Serial.begin(9600);
 analogReference(INTERNAL);
 
}

void loop() 
{
 
 
 float OilPressure = GetOilPressure(analogRead(2));
 Serial.print("OilPressure: ");
 Serial.println(OilPressure, 0);
 delay(1000);
}

float GetOilPressure(int RawADC)
{
 float volt = RawADC * (aref_voltage/1023.0);
 float bar = ((2.1271 * volt) + 5.1075 ) * volt - 0.2561; // optimized
 float psi = bar * 14.7; 
 return psi;
}

into this.

int Vout = 0;
int egtPin = 2;
int egt = 0;
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 



void setup()
{
    lcd.begin(16, 2);
    lcd.setCursor(0,0);
    lcd.print("O:");
    lcd.setCursor(5,0);
    lcd.print((char)223);
    lcd.setCursor(6,0);
    lcd.print("F");
    lcd.setCursor(9,0);
    lcd.print("W:   ");
    lcd.print((char)223);
    lcd.setCursor(15,0);
    lcd.print("F");
    lcd.setCursor(7,1);
    lcd.print("EGT:");
    lcd.setCursor(0,1);
    lcd.print("PSI:");
}
void loop()
{
  
Vout = (analogRead(egtPin) * .004882814);  
egt = (((Vout - 1.25) / 0.005)* 1.8) +32;//egt in F
  
float resistance, voltage, r_fixed, T0, T, R0, B, F, Toil, F1;
T0 = 348.15;//initial temperature for calibration
R0 = 150;//initial resistance for calibration
r_fixed = 138;//known resistance
B = 3947;//B for normalization



while(1)
{
voltage = analogRead(0)*5.0/1024;// Renormalizing from 0 to 5 volts.
resistance = voltage*r_fixed/(5.00-voltage);//find unknown resistance
T = 1/(log(resistance/r_fixed)/B+1/T0)-273.14;//find temperature from unknown ressistance
F = (T * 9 / 5 + 32);

voltage = analogRead(1)*5.0/1024;// Renormalizing from 0 to 5 volts.
resistance = voltage*r_fixed/(5.00-voltage);//find unknown resistance
Toil = 1/(log(resistance/r_fixed)/B+1/T0)-273.14;//find temperature from unknown ressistance
F1 = (Toil * 9 / 5 + 32);



  lcd.setCursor(11,0);
  lcd.print(F,0);
  lcd.setCursor(2,0);
  lcd.print(F1,0 );
  lcd.setCursor(11,1);
  lcd.print(egt);

delay(1000);

}
}