Go Down

### Topic: Comparing 3 values. (Read 2166 times)previous topic - next topic

#### Gin319

##### Jun 24, 2013, 01:37 pmLast 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?

--Gin319

#### Tom Carpenter

#1
##### Jun 24, 2013, 02:03 pm
The simplest way would be:
Code: [Select]
`max(max(X,Y),Z);`
~Tom~

#### MarkT

#2
##### Jun 24, 2013, 02:47 pm
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 will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### Gin319

#3
##### Jun 25, 2013, 12:27 am

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

#4
##### Jun 25, 2013, 12:33 am
You can't directly, you would need to do something like this:
if ((Z > X) && (Z>Y))
{
// Do Z stuff
}

#### Arrch

#5
##### Jun 25, 2013, 01:14 am
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.

#### KrazeyNKrusty

#6
##### Jun 25, 2013, 02:50 am
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

#7
##### Jun 25, 2013, 02:53 am

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

#8
##### Jun 25, 2013, 02:56 am
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

#9
##### Jul 10, 2013, 10:56 am
Thanks for the input guys!
--Gin319

#### MarkT

#10
##### Jul 10, 2013, 02:28 pm

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 will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### aarondc

#11
##### Jul 10, 2013, 02:39 pm
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

#12
##### Jul 10, 2013, 02:53 pm

I called it psuedo code, not compilable code.

It does compile, that's the problem, but isn't a function definition!
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### aarondc

#13
##### Jul 10, 2013, 02:54 pm
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

#14
##### Jul 10, 2013, 02:56 pm
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

Please enter a valid email to subscribe