Background: I am using an analog pressure sensor with a range from 0-100PSI returning voltage values .44v to 4.44v. It's open air (or 0 PSI) voltage on the signal pin is .47 volts. I have a function that subtracts that .47v (to make zero) and then multiplies it by 25 (25 PSI per volt). Due to signal noise or other factors, sometimes my function returns a negative number, which wreaks havoc on my math. I would like to return a value of 0 when my offset math returns a negative number, but I'm having a problem with it.
The code that I wrote that doesn't work:
double EngineOilPressure() {
//insert oil pressure code here
int reading = analogRead(sensorPin3);
float voltage = reading * 5.0;
voltage /=1024.0;
Serial.print(voltage); Serial.println(" volts");
if (voltage-.47 < 0)
{
float voltadj = 0;
}
else
{
float voltadj = (voltage-.47);
}
float pressureP = voltadj * 25;
return PSIToPascal(pressureP);
}
The error that I get is
dynamic_engine.pde: In function 'double EngineOilPressure()':
dynamic_engine.pde:41:23: error: 'voltadj' was not declared in this scope
Error compiling.
The code that works but sometimes returns a very wrong value:
double EngineOilPressure() {
int reading = analogRead(sensorPin3);
float voltage = reading * 5.0;
voltage /=1024.0;
Serial.print(voltage); Serial.println(" volts");
float pressureP = (voltage - .47) * 25;
return PSIToPascal(pressureP);
}