Pages: [1] 2   Go Down
Author Topic: Comparing 3 values.  (Read 1008 times)
0 Members and 1 Guest are viewing this topic.
Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-grin
« Last Edit: June 24, 2013, 06:39:37 am by Gin319 » Logged

--Gin319

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The simplest way would be:
Code:
max(max(X,Y),Z);
Logged

~Tom~

0
Offline Offline
Shannon Member
****
Karma: 207
Posts: 12200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or directly coded as:
Code:
  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:
if (new_value > largest_value)
{
  largest_value = new_value ;
  trigger_action () ;
}
Logged

[ I won't respond to messages, use the forum please ]

Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

--Gin319

Offline Offline
Edison Member
*
Karma: 33
Posts: 1447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
}
Logged

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
bool (x_is_biggest(int x, int y, int z)) {
    return ((x > y) && (x > z));
}
Logged

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

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the input guys! smiley
Logged

--Gin319

0
Offline Offline
Shannon Member
****
Karma: 207
Posts: 12200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
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:
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.
Logged

[ I won't respond to messages, use the forum please ]

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I called it psuedo code, not compilable code.

Logged

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

0
Offline Offline
Shannon Member
****
Karma: 207
Posts: 12200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I called it psuedo code, not compilable code.

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

[ I won't respond to messages, use the forum please ]

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pages: [1] 2   Go Up
Jump to: