user18878: 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);
}


user4143: Another method, though one that is quite cryptic to understand, would be:
float pressureP = voltage > 0.47 ? (voltage - 0.47) * 25 : 0;

That is known (by me at least) as an inline conditional.  The ? acts as an if operator, and the : as an else.  Basically it's saying:
Voltage is greater than 0.47
Is that true?
    then subtract 0.47 and multiply by 25 and assign it
otherwise:
    assign 0

The format is simply:
value = {condition} ? {true value or expression} : {false value or expression};

If can be quite useful when you want to conditionally calculate or assign something.  @jwpat7 makes mention of a max() function above.  That max() function can itself be written with this inline conditional format:
maxval = a > b ? a : b;

Or: If a is greater than b then assign a otherwise assign b.

user18478: The Arduino way is to use constrain
float pressureP = constrain ((voltage - 0.47) * 25 , 0 [The Maximum your code can handle]);

This will return the value ((voltage - 0.47) * 25) but never less than 0 and never more than The maximum you provide.

user3917: The scope of each of the two different and unrelated voltadj variables that your code (as below) declares is limited to within its enclosing set of braces.
if (voltage-.47 < 0)
{
    float voltadj = 0;
}
else
{
    float voltadj = (voltage-.47);
}

Instead perhaps say
float voltadj = voltage-.47;
if (voltadj < 0) voltadj = 0;

or perhaps
float voltadj = voltage < .47? 0 : voltage-.47;

or
float voltadj = constrain(voltage-0.47, 0.0, 5.0);

or 
float voltadj = max(0.0, voltage-0.47);

Also see max() and constrain() reference pages at arduino.cc.

18878: <p>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. </p>

<p>The code that I wrote that doesn't work: </p>

<pre><code>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 &lt; 0)
    {
        float voltadj = 0;
    }
    else
    {
        float voltadj = (voltage-.47);
    }

    float pressureP = voltadj * 25;
    return PSIToPascal(pressureP);
}
</code></pre>

<p>The error that I get is </p>

<pre><code>dynamic_engine.pde: In function 'double EngineOilPressure()':
dynamic_engine.pde:41:23: error: 'voltadj' was not declared in this scope
Error compiling.
</code></pre>

<p>The code that works but sometimes returns a very wrong value: </p>

<pre><code>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);
}
</code></pre>
 4143: <p>Another method, though one that is quite cryptic to understand, would be:</p>

<pre><code>float pressureP = voltage &gt; 0.47 ? (voltage - 0.47) * 25 : 0;
</code></pre>

<p>That is known (by me at least) as an <em>inline conditional</em>.  The <code>?</code> acts as an <code>if</code> operator, and the <code>:</code> as an else.  Basically it's saying:</p>

<pre><code>Voltage is greater than 0.47
Is that true?
    then subtract 0.47 and multiply by 25 and assign it
otherwise:
    assign 0
</code></pre>

<p>The format is simply:</p>

<pre><code>value = {condition} ? {true value or expression} : {false value or expression};
</code></pre>

<p>If can be quite useful when you want to conditionally calculate or assign something.  @jwpat7 makes mention of a <code>max()</code> function above.  That <code>max()</code> function can itself be written with this inline conditional format:</p>

<pre><code>maxval = a &gt; b ? a : b;
</code></pre>

<p>Or: If <code>a</code> is greater than <code>b</code> then assign <code>a</code> otherwise assign <code>b</code>.</p>
 18478: <p>The Arduino way is to use <a href="https://www.arduino.cc/en/Reference/Constrain" rel="nofollow">constrain</a></p>

<pre><code>float pressureP = constrain ((voltage - 0.47) * 25 , 0 [The Maximum your code can handle]);
</code></pre>

<p>This will return the value ((voltage - 0.47) * 25) but never less than 0 and never more than The maximum you provide.</p>
 3917: <p>The scope of each of the two different and unrelated <code>voltadj</code> variables that your code (as below) declares is limited to within its enclosing set of braces.</p>

<pre><code>if (voltage-.47 &lt; 0)
{
    float voltadj = 0;
}
else
{
    float voltadj = (voltage-.47);
}
</code></pre>

<p>Instead perhaps say</p>

<pre><code>float voltadj = voltage-.47;
if (voltadj &lt; 0) voltadj = 0;
</code></pre>

<p>or perhaps</p>

<pre><code>float voltadj = voltage &lt; .47? 0 : voltage-.47;
</code></pre>

<p>or</p>

<pre><code>float voltadj = constrain(voltage-0.47, 0.0, 5.0);
</code></pre>

<p>or </p>

<pre><code>float voltadj = max(0.0, voltage-0.47);
</code></pre>

<p>Also see <a href="https://www.arduino.cc/en/Reference/Max">max()</a> and <a href="https://www.arduino.cc/en/Reference/Constrain">constrain()</a> reference pages at arduino.cc.</p>
