 # Comparing 3 values.

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

The simplest way would be:

``````max(max(X,Y),Z);
``````

Or directly coded as:

``````  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.

``````if (new_value > largest_value)
{
largest_value = new_value ;
trigger_action () ;
}
``````

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.

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

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.

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 }

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.

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:

``````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

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

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.

Thanks for the input guys! :)

aarondc: ``` 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):

``````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 called it psuedo code, not compilable code.

aarondc: I called it psuedo code, not compilable code.

It does compile, that's the problem, but isn't a function definition!

And it is also, as I stated, pseudo code. The fact that it compiles at all is simply dumb luck.

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.

bool however produces smaller and thus faster code.

As boolean is typedef by Arduino.h to a unsigned char (uint8_t) the compiler can miss warning you about placing non true/false or 1/0 values.