Best way to compare multiple values?

Hello,
I am looking for a better way to do compares of multiple int values, returning the value with the highest (or lowest) values in an efficient manner. The way that I am doing it now is as follows:

This scans 3 different positions, and finds closest object-

if (dist_left < dist_right) { 
    if (dist_left < dist_center) {
      driveit(500, 8); //rotate left 1/8
    }
  }
  if (dist_right < dist_left) { 
    if (dist_right < dist_center) {
      driveit(500, 7); //rotate right 1/8
    }  
  }
  if (dist_center < dist_right) { 
    if (dist_center < dist_left) {
      if (dist_left < dist_right) {
        driveit(500, 8); //rotate left 1/2
      }
    }

The problem with this is that if i want to add more scan ranges, this particular style of comparison gets incredibly complex. It seems to me that there is a much simpler way to do this, I just cant figure it out at the moment.

Thanks for any help!
:slight_smile:

edited: to put code in code window

If you stored your distances in an integer array you could write a function that was passed a pointer to the array and the number of elements in the array. Your function could loop through all the elements and compare each to a highestValue variable (initially set to 0, and set to any value higher than itself). When you loop is finished with all the elements you can return highestValue .

int getHighestValue( int * elements, int count){
int highestValue = 0;
   // your code that does the loop and compare goes here?.
 return highestValue;
}

ooh - neat idea, thank you! :slight_smile:

I would still want to know which integer was related to each scan point, so to play with that idea a bit:

char sparray [10]; //name of scanpoint
int spvarray [10]; //value of scanpoint

//code for setup
sparray[1]=sp1; //etc to fill array with the 10 scanpoints

//code during data acquisition
spvarray[1]=scanrange(); //etc to fill array with the 10 scanpoint values

//code during object avoidance
nearestobj = (sparray[getHighestValue(spvarray, 10)]) //finds scanpoint with highest value
//direction decision code goes here
 
int getHighestValue ( int * sparray, int count){
int highestValue = 0;
int count1 = 0;
if (count1 < count) {
  if (sparray[count1] > highestValue) {highestValue = count1}
count1++
}

I think I am borking something up in the above, just typing it here and not in the arduino environment, and am still new to this.
Does that look like it would work? I need to play with arrays more...
Thanks!

I have not tested it but I hope this gets you closer to what you want:

char * spNames[10] = {"item0","item1","item2","item3","item4","item5","item6","item7","item8", "item9"}; //name of scanpoint
int spvarray [10]; //value of scanpoint


long randNumber;

void setup(){
  Serial.begin(9600);
  for(int i=0; i < 10; i++){  
    spvarray[i] = (int)random(1000); //etc to fill array with the 10 scanpoints with random numbers between 0 and 1000
    Serial.print(i,DEC);
    Serial.print("=");
    Serial.println(spvarray[i],DEC);  // display the values on the serial terminal
  }
}

void loop() {

  int nearestobj = getHighestValue(spvarray, 10); //finds scanpoint with highest value
  Serial.print("nearest object is ");
  
  Serial.println( spNames[nearestobj]); //finds scanpoint with highest value
  delay(5000);
}


// this function returns the index of the highest value in the given array
int getHighestValue( int * array, int count){
  int highestValueIndex = 0;  
  for(int i=1; i < count; i++){
    if (array[i] > array[highestValueIndex] )
      highestValueIndex = i;
  }
  return highestValueIndex;
}

Thank you very much
This is great - this logic should get me right where I want to be, and allow me to extend it much easier than I thought I would be able to.
/bow to the code guru! :slight_smile: