Combine parameters in decisions

For general Flowcode discussion that does not belong in the other sections.
Post Reply
MJU20
Posts: 238
http://meble-kuchenne.info.pl
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Combine parameters in decisions

Post 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)

medelec35
Matrix Staff
Posts: 1432
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 506 times
Been thanked: 469 times

Re: Combine parameters in decisions

Post 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.
Martin

MJU20
Posts: 238
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Combine parameters in decisions

Post by MJU20 »

Oh my!!

This is right!

Thank you Medelec35..

medelec35
Matrix Staff
Posts: 1432
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 506 times
Been thanked: 469 times

Re: Combine parameters in decisions

Post by medelec35 »

You're welcome, as that can catch a lot of people out.
Martin

MJU20
Posts: 238
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Combine parameters in decisions

Post by MJU20 »

Strange:

Code: Select all

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

medelec35
Matrix Staff
Posts: 1432
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 506 times
Been thanked: 469 times

Re: Combine parameters in decisions

Post 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))
Martin

MJU20
Posts: 238
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Combine parameters in decisions

Post 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 :)

LeighM
Valued Contributor
Posts: 394
Joined: Mon Dec 07, 2020 1:00 pm
Has thanked: 69 times
Been thanked: 208 times

Re: Combine parameters in decisions

Post by LeighM »

From your description, I think you need

Code: Select all

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

Steve-Matrix
Matrix Staff
Posts: 1234
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Combine parameters in decisions

Post 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.
Attachments
floating point comparison.fcfx
(7.91 KiB) Downloaded 67 times

Post Reply