Nesting min() functions

Hi, After searching for a bit, I found there is no helpful answer for if you can nest min() functions. I've seen that min(a+3, 2) math inside a min() function does not yield the right results, but would nesting work? I can't test this yet though, and I am curious. If it doesn't work what would be another solution beside ten different variables. Here is my nest: minvoldif = min(voldif[0], min(voldif[1], min(voldif[2], min(voldif[3], min(voldif[4], min(voldif[5], min(voldif[6], min(voldif[7], min(voldif[8], voldif[9]))))))))); Don't mind the weird variable names. It is volume lest difference. For reference here is the rest of the code. Thanks for any help on this.

//LED OUTPUT
#define DATA 6
#define LATCH 8
#define CLOCK 10
int valarray[] = {102, 204, 306, 409, 511, 613, 716, 818, 920, 1023};
int shiftbin[] = {B01000000, B01100000, B01110000, B01111000, B01111100, B01111110, B01111111, B11111111, B11111111, B11111111};
int voldif[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int binary = 0;
int minvoldif = 0;
//volume
int setvolume = 0;
int volume = 0;
//in
int button = 0;
int pot = 0;
int beforepot = 0;
int rep = 0;
//relay
bool latch = false;

void setup() {
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  Serial.begin(9600);
}

void queryout() {
  voldif[0] = abs(valarray[0] - volume);
  voldif[1] = abs(valarray[1] - volume);
  voldif[2] = abs(valarray[2] - volume);
  voldif[3] = abs(valarray[3] - volume);
  voldif[4] = abs(valarray[4] - volume);
  voldif[5] = abs(valarray[5] - volume);
  voldif[6] = abs(valarray[6] - volume);
  voldif[7] = abs(valarray[7] - volume);
  voldif[8] = abs(valarray[8] - volume);
  voldif[9] = abs(valarray[9] - volume);
  minvoldif = min(voldif[0], min(voldif[1], min(voldif[2], min(voldif[3], min(voldif[4], min(voldif[5], min(voldif[6], min(voldif[7], min(voldif[8], voldif[9])))))))));
  Serial.print("Min: " + minvoldif); //Logging
  binary = valarray[voldif[minvoldif]];
  binary = shiftbin[binary];
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, LSBFIRST, binary);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  if (valarray[voldif[minvoldif]] == 9) {
    digitalWrite(9, HIGH);
  }
  if (valarray[voldif[minvoldif]] == 10) {
    digitalWrite(9, HIGH);
    digitalWrite(11, HIGH);
  }
  digitalWrite(LATCH, HIGH);
  if (latch == true) {
    digitalWrite(12, HIGH);
  }
  else {
    digitalWrite(12, LOW);
  }
}

void relay() {
  if (latch == false) {
    if (volume > setvolume) {
      digitalWrite(13, HIGH);
      Serial.println("Engaged Drop"); //Logging
    }
    else {
      digitalWrite(13, LOW);
    }
  }
  else {
    digitalWrite(13, LOW);
  }
}

void queryin() {
  volume = analogRead(0);
  button = analogRead(1);
  if (button > 100) {
    if (latch == false) {
      latch == true;
      Serial.println("Latch on"); //Logging
    }
    else {
      latch = false;
    }
    digitalWrite(12, HIGH);
    delay(250);
    digitalWrite(12, LOW);
    delay(250);
    digitalWrite(12, HIGH);
    delay(250);
    digitalWrite(12, LOW);
    delay(250);
  }
  pot = analogRead(3);
  if (pot != beforepot) {
    do {
      pot = analogRead(3);
      setvolume = pot;
      if (pot != beforepot) {
        beforepot = pot;
        rep = 0;
      }
      else {
        rep = rep + 1;
      }
      voldif[0] = abs(valarray[0] - setvolume);
      voldif[1] = abs(valarray[1] - setvolume);
      voldif[2] = abs(valarray[2] - setvolume);
      voldif[3] = abs(valarray[3] - setvolume);
      voldif[4] = abs(valarray[4] - setvolume);
      voldif[5] = abs(valarray[5] - setvolume);
      voldif[6] = abs(valarray[6] - setvolume);
      voldif[7] = abs(valarray[7] - setvolume);
      voldif[8] = abs(valarray[8] - setvolume);
      voldif[9] = abs(valarray[9] - setvolume);
      minvoldif = min(voldif[0], min(voldif[1], min(voldif[2], min(voldif[3], min(voldif[4], min(voldif[5], min(voldif[6], min(voldif[7], min(voldif[8], voldif[9])))))))));
      Serial.print("Min: " + minvoldif); //Logging
      binary = valarray[voldif[minvoldif]];
      binary = shiftbin[binary];
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLOCK, LSBFIRST, binary);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      if (valarray[voldif[minvoldif]] == 9) {
        digitalWrite(9, HIGH);
      }
      if (valarray[voldif[minvoldif]] == 10) {
        digitalWrite(9, HIGH);
        digitalWrite(11, HIGH);
      }

    } while (rep < 100);
  }
}

void loop() {
  queryin();
  queryout();
  relay();
}

math inside a min() function does not yield the right results

Prove it. Post a small sketch (in code tags) which demonstrates it.

what would be another solution

A for loop.

Pete

el_supremo:
Prove it. Post a small sketch (in code tags) which demonstrates it.
A for loop.

Pete

As for proving it, I have no proof but I found it on a Stack Overflow forum on how to use min().(Also I was just asking because I can't test my code yet. :-\ )
When I can try it, I will and I will let you know the answer.
Thanks for the loop answer but how could you set it up exactly. Just wondering...

I have no proof

But you said it doesn't yield the right results. Why do you think that?

declare a variable min_value and set it to voldif[0].
Then use a for loop to go through the remaining elements of the array and if one is less than main_value replace min_value.

Pete

el_supremo:
But you said it doesn't yield the right results. Why do you think that?

declare a variable min_value and set it to voldif[0].
Then use a for loop to go through the remaining elements of the array and if one is less than main_value replace min_value.

Pete

Thank you for the loop help.
As I said earlier, "As for proving it, I have no proof but I found it on a Stack Overflow forum on how to use min().(Also I was just asking because I can't test my code yet. :-\ )"

As for proving it, I have no proof but I found it on a Stack Overflow forum on how to use min().

I suspect that the Stack Overflow forum was taking about the potential side effects of having math operations performed on the macro arguments, not about whether or not min() worked. And, I suspect that the thread was about ++ or -- operations, not addition or subtraction.

PaulS:
I suspect that the Stack Overflow forum was taking about the potential side effects of having math operations performed on the macro arguments, not about whether or not min() worked. And, I suspect that the thread was about ++ or -- operations, not addition or subtraction.

I looked back at the article you are right. Thanks for the help!

int minArray(int a[], int len) {
  int min = 32767;
  for(int i=0; i<len; i++) {
    if(a[i]<min) min = a[i];
  }
  return min;
}

Edit: fixed the order of items in the for()

Thank you for the code I will most likely use it if the nesting fails. Thanks!

That code won't work. The for loop isn't right. Try this:

int minArray(int a[], int len) {
  int min = a[0];
  for(int i=1; i < len; i++) {
    if(a[i]<min) min = a[i];
  }
  return min;
}

Note that this will work even if it is compiled on a processor where an 'int' is 8, 16, 32, or 64 bits.

Pete