Comparing multiple Arduino inputs

Hi,

 

This is probably very simple, but is there an easy way to find which of multiple arduino inputs is greatest/least?

All I can think of is using lots of  if/else's. I tried googling it but found no relevent results.

 

I am thinking of between 10-16 inputs, so if/ else's would not be very practical.

 

Thanks, 

When you ask about greatest/least,

do you mean you are looking at analog lines or digital? I find it difficult to believe an arduino has more than at best maybe 10 analog lines and I believe that is pushing it (I was wrong the 2560 has 16 analog pins. For the sake of argument, try something along these lines (Disclaimer: I don’t know arduino)

Declare variable for highest: int highest = 0;
Declare variable for lowest: int lowest = 1023; //1023 for 10bit ADC, 255 for 8bit (I hope :slight_smile: )
Declare variable for highestPin and lowestPin: int highestPin, lowestPin;
Declare variable for number of analog pins: int numberOfAnalogPins = 16;
Declare an array for your analog pin numbers: int aiPins(numberOfAnalogPins)={ ‘enter the pin numbers here separated by commas’ };
Declare an array for your analog pins: int analogInputPins(numberOfAnalogPins);
Loop through the 16 pins: for(i=0;i<numberOfAnalogPins;++i)
{ analogInputPins(i) = analogRead( aiPins(i) );
   if (analogInputPins(i) > highest) {
      highestPin = i; }
   if (analogInputPins(i) < lowest) {
      lowestPin = i; }
}

Your highest and lowest pin numbers should now be in the highestPin and lowestPin variables.

That looks good,

That looks good, thanks, but I think it may be missing something,

 

Declare variable for highest: int highest = 0;
Declare variable for lowest: int lowest = 1023; //1023 for 10bit ADC, 255 for 8bit (I hope :slight_smile: )
Declare variable for highestPin and lowestPin: int highestPin, lowestPin;
Declare variable for number of analog pins: int numberOfAnalogPins = 16;
Declare an array for your analog pin numbers: int aiPins(numberOfAnalogPins)={ ‘enter the pin numbers here separated by commas’ };
Declare an array for your analog pins: int analogInputPins(numberOfAnalogPins);
Loop through the 16 pins: for(i=0;i<numberOfAnalogPins;++i) 
{ analogInputPins(i) = analogRead( aiPins(i) );
   if (analogInputPins(i) > highest) {
      highestPin = i;

      highest=analogInputPins(i);  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Here

   }

   if (analogInputPins(i) < lowest) {
      lowestPin = i;

          lowest=analogInputPins(i);  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<And Here

    }
}

Without these, wouldn’t both highest+lowestPin Both end up as 16? (Provided 16 is not 1023 or 0)

Correct me if I am wrong.

Thanks for your input, looks promising

Cheers,

You are spot on.

All I can really say is, I saved you a whole batch of if/then statements. :stuck_out_tongue:
And, all you had to do was debug my semi quick code. :) 

Thanks

Thanks heaps Birdmun,

You have saved me alot of trouble. Thanks for the quick replys.