IF Statement Tolerance in variable?

So I have this if statement (below) and a certain part of it to have a tolerance e.g. +/- 0.1 but not sure how to integrate that...

if ( psi < setpoint ) && (psi < 0.18 || psi > 0.24))

What I want...

if ( psi < setpoint +/- 0.1) && (psi < 0.18 || psi > 0.24))

So, you want to test that psi is more than setpoint - 0.1 and less than setpoint + 0.1. Hmmm, let me think how you'd do that...

Just do it with two tests like you did in the second half that line...

Actually, wait, this is silly. The test is if psi is less than setpoint +/-1... what's the cutoff? If psi is less than setpoint-1, you know it's less than setpoint+1 and everything in between... So you can just pick a cutoff and get the same results

Sorry, I can see what you mean... Basically I have a sensor reading a value and then this statement decides whether to activate a pump or not. But its too sensitive as the sensor constantly fluctuates around 0.05 and in that 0.1 +/- range I don't want the pump to do anything...

I want it to be - IF PSI is SMALLER THAN (Setpoint +/- 0.1) AND PSI it NOT between 0.18 and 0.29

THEN activate pump

else deactivate pump

I want it to be - IF PSI is SMALLER THAN (Setpoint +/- 0.1) AND PSI it NOT between 0.18 and 0.29

Let's work on that requirement statement. Do you mean "if psi < setpoint + 0.1" or do you mean "if psi < setpoint - 0.1" or do you mean something completely different?

Ahhh what I want basically won't work... I can see it now basically the setpoint, once its set it doesn't move. the PSI is the reading being lively read from the board, I only want to it to activate the pump IF the PSI fluctuates more than 0.1 in either + or - from the setpoint.... Does that make more sense? I just don't know how to go about that...

Does that make more sense?

No. Why would the setpoint change?

I suspect, though, that want you want is to do something if the measured value is between setpoint - 0.1 and setpoint + 0.1

if(psi > setpoint + 0.1 && psi < setpoint - 0.1)
{
}

Within the body of that block, you can test that psi is not too low or too high, but how that is possible, if the reading is close to the setpoint is a mystery to me.

The setpoint doesn't change. The setpoint is a set pressure, the sensor is constantly reading the pressure but the pressure constantly fluctuates between 0.05 either way (plus and minus).. I ONLY want the pump to activate if the ACTUAL pressure read from the sensor deviates too much from the setpoint (0.1)... Im not sure how I can explain it in any other way :frowning: thanks for your help though!

Im not sure how I can explain it in any other way

Have you tried the code I showed in the previous reply? Does it, or does it not, do just what you want?

Maybe what you need to do is read the pressure several times, and average the readings, to get something more stable.

I'll try it and let you know! Looks like it'll work.. Thank you :slight_smile:

Try using the fabs function.

fabs(a - b) will give you the "distance" between a and b.

Examples:

fabs(5.0 - 4.9) will give you 0.1

fabs(8.3 - 8.5) will give you 0.2

More info here: http://www.cplusplus.com/reference/cmath/fabs/

lloydowen, a setpoint is a setpoint.

If you're happy with setpoint + 0.1 then your setpoint is setpoint+0.1.

Paul has been hinting at this a lot ! I thought maybe saying it in other words might help...

When you say "But its too sensitive as the sensor constantly fluctuates around 0.05 and in that 0.1 +/- range I don't want the pump to do anything..."

It makes me think that what you're really wanting to implement is a hysteresis/deadband/Schmitt trigger - google those terms...

1:1:
It makes me think that what you're really wanting to implement is a hysteresis/deadband/Schmitt trigger - google those terms...

Exactly.