Go Down

Topic: Comparing 3 values. (Read 1 time) previous topic - next topic

Gin319

Jun 24, 2013, 01:37 pm Last Edit: Jun 24, 2013, 01:39 pm by Gin319 Reason: 1
Guys, let's say i want to compare 3 values, from measurements, X, Y and Z. And I want to get the largest value of the 3 variables. How do I approach this problem with the arduino to do an action when the greatest value is read?

Thanks in advance for your replies. :D
--Gin319

Tom Carpenter

The simplest way would be:
Code: [Select]
max(max(X,Y),Z);
~Tom~

MarkT

Or directly coded as:
Code: [Select]

  if (a > b)
    return c > a ? c : a ;
  else
    return c > b ? c : b ;


Generally for an array of values you have two options:

Either scan for the greatest member in the array with a for loop.

Or maintain the array in sorted order, then the largest and smallest are at the ends - this is particularly
suitable if the data changes occasionally and you want to get at max / min values all the time - you use
bubble-sort or similar to resort the array as new values arrive.

Also your question implies you want to trigger an action when a new value larger than any previous
value is read - this is a different question really, since you just need to remember one value, namely the
largest so far, in order to do this.
Code: [Select]

if (new_value > largest_value)
{
  largest_value = new_value ;
  trigger_action () ;
}
[ I won't respond to messages, use the forum please ]

Gin319

thanks for your replies!

im a bit confused. the "max" command worked, but how could i return the variable with the largest value? for example:

x = 1;
y = 3;
z = 4;

since z is the greatest reading, then it would do a certain action.
--Gin319

KeithRB

You can't directly, you would need to do something like this:
if ((Z > X) && (Z>Y))
{
    // Do Z stuff
}

Arrch

Perhaps it would be better to provide us with more information on WHAT you're trying to, rather than HOW you're trying to do it. Some context will help quite a bit.

Not sure what you want to happen if two or more of your variables are equal to the largest value.
Quote
int largestValue = max(max(X,Y),Z);

if(X==largestValue){
  //do X stuff
}

if(Y==largestValue){
  //do Y stuff
}

if(Z==largestValue){
  //do Z stuff
}

aarondc


thanks for your replies!

im a bit confused. the "max" command worked, but how could i return the variable with the largest value? for example:

x = 1;
y = 3;
z = 4;

since z is the greatest reading, then it would do a certain action.


Should have posted last night - I thought it strange to want the max value of 3 axes but not know which one.

There's a host of ways of doing it, but psuedo code can look like this:
Code: [Select]

if (x_is_biggest(x, y, z)) {
    // do something for x
}
else if (y_is_biggest(x, y, z)) {
    // do something for  y
}
else if (z_is_biggest(x, y, z)) {
    // do something for z
}


keeping in mind you will need to test edge cases for scenarios like x == y == z and combinations thereof.

your *_is_biggest functions will look like
Code: [Select]

bool (x_is_biggest(int x, int y, int z)) {
    return ((x > y) && (x > z));
}
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

aarondc

if your x,y, z are truly axes, you can also possibly see negative values that are "biggest" in terms of primary direction, so use abs() function to return everything to a common scale for comparison.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

Gin319

Thanks for the input guys! :)
--Gin319

MarkT



Code: [Select]

bool (x_is_biggest(int x, int y, int z)) {
    return ((x > y) && (x > z));
}


Too many parantheses!  Certainly you need to loose the cast to bool and change to a function definition (boolean
is the preferred type for Arduino code I think):
Code: [Select]

boolean x_is_biggest (int x, int y, int z) {
    return x > y && x > z ;
}

return doesn't take parentheses like if and while do, lots of people think it does, also && and || bind lower than comparisons.
[ I won't respond to messages, use the forum please ]

aarondc

I called it psuedo code, not compilable code.

Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

MarkT


I called it psuedo code, not compilable code.


It does compile, that's the problem, but isn't a function definition!
[ I won't respond to messages, use the forum please ]

aarondc

And it is also, as I stated, pseudo code. The fact that it compiles at all is simply dumb luck.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

aarondc

OP: if you try out my suggestion and it doesn't work, feel free to post back and I am sure we will be able to sort it out.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

Go Up