Page 1 of 1

Combine parameters in decisions

Posted: Sun Jan 09, 2022 5:41 pm
by MJU20
I want to combine parameters in one decision.
I think I did this in the past but suddenly I don't get it to work.

Example:
I want to check a variable against a previous value.
The new value is Temp and the old value is TempOld.
If the new value is 4°C higher OR 4°C lower then the old value, the value must be rejected with a decision.

How do I combine the > and < in 1 line in a decision?

This is how I did it, but it doesn't work.

Code: Select all

Temp < (TempOld - 4) OR Temp > (TempOld + 4)

Re: Combine parameters in decisions

Posted: Sun Jan 09, 2022 5:55 pm
by medelec35
Hi
The issue is OR is bitwise, which is not what you are after.
Take a look at this post as it should help.

Re: Combine parameters in decisions

Posted: Sun Jan 09, 2022 6:28 pm
by MJU20
Oh my!!

This is right!

Thank you Medelec35..

Re: Combine parameters in decisions

Posted: Sun Jan 09, 2022 6:59 pm
by medelec35
You're welcome, as that can catch a lot of people out.

Re: Combine parameters in decisions

Posted: Mon Jan 10, 2022 5:37 am
by MJU20
Strange:

Code: Select all

Temp < (TempOld - 4) || Temp > (TempOld + 4)
Did not work...

Re: Combine parameters in decisions

Posted: Mon Jan 10, 2022 8:36 am
by medelec35
I have just read your example again and you will need

Code: Select all

Temp <= (TempOld - 4) || Temp >= (TempOld + 4)
Without the

Code: Select all

=
it will be 5 or more.
Is that the issue you have?
Note: it is good practice to have each equation enclosed within brackets so you could try

Code: Select all

(Temp <= (TempOld - 4))|| (Temp >= (TempOld + 4))

Re: Combine parameters in decisions

Posted: Tue Jan 11, 2022 5:42 pm
by MJU20
Thanks again medelec35.

But this confuses me more and more.
I've used combined variables often in Flowcode, but these days all my attempts fail.

Later in the chart I want to check if a temperature is the same as the previous value AND if a counter (byte) isn't larger then a certain value.
But again, this must be my lack of intelligence, but this doesn't work either.

Is it because I combine a float (temp/tempold) with a byte variable (counter)?

Code: Select all

((Temp != TempOld) && (counter != 2))
So this should read: If NOT Temp is the same as TempOld AND counter is not 2.. then...

The way I read it, the first part, if that is true (so they are not the same) this part is true
AND this with the second part: if counter is not 2, then this is also true.

So this should be inverse to this (in my case couter is reset to 0 everytime):

Code: Select all

((Temp == TempOld) && (counter > 2))
I think I must write it all down somewhat more, I'm getting old :)

Re: Combine parameters in decisions

Posted: Tue Jan 11, 2022 6:05 pm
by LeighM
From your description, I think you need

Code: Select all

 ((Temp == TempOld) && (counter < 2)) 

Re: Combine parameters in decisions

Posted: Tue Jan 11, 2022 6:20 pm
by Steve-Matrix
(Leigh beat me to it, but here's my answer anyway...)
MJU20 wrote:
Tue Jan 11, 2022 5:42 pm
Later in the chart I want to check if a temperature is the same as the previous value AND if a counter (byte) isn't larger then a certain value.
This would be:

Code: Select all

if ((Temp == TempOld) && (counter <= value))
MJU20 wrote:
Tue Jan 11, 2022 5:42 pm
Is it because I combine a float (temp/tempold) with a byte variable (counter)?
The two expressions in the inner brackets are evaluated separately, so they won't 'interfere' with each other.

However, the problem could be the comparison between float values. For example, 5.000000001 does not equal 5.000000000, but you might want your program to treat them as equal.

One way to check if floating point values are equal is to subtract them from each other and check that the result is close to zero. Something like this would be true if the values 'float1' and 'float2' are close enough to consider them as the same value:

Code: Select all

if (fabs(float1 - float2) < 0.000001)
"fabs" is a function that ensures the parameter is positive, e.g. fabs(-0.3) = 0.3

The value "0.000001" is often called "epsilon" and needs to be set to a value specific to your program - i.e. it depends how close the values need to be to be seen as the same, and this depends on the accuracy of your measurements, their expected ranges and perhaps any other sources of error in your measurements.

The attached program shows this in Flowcode.