How to read maximum value from 4 input

i have 4 input sensor on A0-A4

how to read only the maximum value between these 4 input?

if max(A0,A1,A2,A3) //any maxim value //then go to next line else //do nothing

dut:
i have 4 input sensor on A0-A4

how to read only the maximum value between these 4 input?

A0-A4, that makes 5…

anyway TIMTOWTDI

``````int maxval=max(max(analogRead(A0), analogRead(A1)), max(analogRead(A2), analogRead(A3)));
if (maxval /* > smth? */) {
}
``````

more readable:

``````int maxval=analogRead(A0);
maxval=max(maxval,analogRead(A1)); // you can loop that
maxval=max(maxval,analogRead(A2));
maxval=max(maxval,analogRead(A3));
if (maxval /* > smth? */) {
}
``````

you can also write a macro using define (as max already is), writing a function, writing a max class, using variable args functions etc.etc.

``````maxval=max(maxval,analogRead(A1));
``````

Expand the macro "max" and see why you may not want to do this.

AWOL: ``` maxval=max(maxval,analogRead(A1)); ```

Expand the macro "max" and see why you may not want to do this.

darn, you are right... :grin:

so, after AWOLs observation, put the values in an array and then use the max macro

Or just write a function (template?) version of the max macro.

scjurgen: so, after AWOLs observation, put the values in an array and then use the max macro

how to do that? like this

readmax[3]={analogread(A0),analogread(A1),analogread(A2),analogread(A3),};

then how to know which one is the maximum value?

how to do that? like this

No. The maximum value will be one of the values in the array.

``````int readVals[4];

void loop()
{
for(int i=0; i<4; i++)
{
readVals[i] = analogRead(i);
}
int maxVal = max(readVals[0], readVals[1]);
maxVal = max(maxVal, readVals[2]);
maxVal = max(maxVal, readVals[3]);
}
``````

The use of an array is not strictly necessary, but using analogRead() in the max() call is not a good idea.

`````` readmax[3]={analogread(A0),analogread(A1),analogread(A2),analogread(A3),};
``````

Most certainly not like that - the compiler won't like lots of thing there

PaulS:

how to do that? like this

No. The maximum value will be one of the values in the array.

``````int readVals[4];
``````

void loop()
{
for(int i=0; i<4; i++)
{
readVals[i] = analogRead(i);
}
int maxVal = max(readVals[0], readVals[1]);
maxVal = max(maxVal, readVals[2]);
maxVal = max(maxVal, readVals[3]);
}

``````

The use of an array is not strictly necessary, but using analogRead() in the max() call is not a good idea.
``````

how to know which input the higher if like this?

The highest value will be in maxVal.
However, it will not tell you which input that value came from (if that is important)

``````void loop()
{
int maxVal = -1;
int maxIndex;
for(int i=0; i<4; i++)  {
int val = analogRead(i);
if (val > maxVal) {
maxVal = val;
maxIndex = i;
}
}
}
``````

Well -- there is another approach...

http://www.softpanorama.org/Algorithms/sorting.shtml

Having a handy dandy routine ready will allow you to pick off the min, max and middle (mode) -- assuming an odd number of values...

There are many ways to skin a cat -- it's just that there are not a lot of good ways to skin a cat.

Any way of skinning a cat is a good one, because it implies one more cat that can't s**t in my garden.

the min, max and middle (mode)

Or even the median?

@dut

What do you want the "system" to do if there are two identical max values e.g. pin 2 and pin 3... ;) or three or they are all max (or zero) ?

Can you tell us more about the type of devices you want to analogRead();?

AWOL:

``````void loop()
``````

{
int maxVal = -1;
int maxIndex;
for(int i=0; i<4; i++)  {
int val = analogRead(i);
if (val > maxVal) {
maxVal = val;
maxIndex = i;
}
}
}

this isn’t working, it only show value 1 and 0.

which one is the max value is important for this coding,
i want to know where the most max value between these 4 input…
how?

IN the code the maxindex gives the pin where the max value was found.

Can you tell more about your application? What do you try to accomplish? maybe there are smarter ways....

robtillaart: @dut

What do you want the "system" to do if there are two identical max values e.g. pin 2 and pin 3... ;) or three or they are all max (or zero) ?

Can you tell us more about the type of devices you want to analogRead();?

ok here is

analogRead = taking 4 input from LDR sensor then the led on pin 9-12 act like the which one the sensor is max value.. eg if the LDR on pin A0 is max, then LED on pin 9 is High then if A1 is the maximum, LED on pin 10 is High and so on

OK makes a bit sense but what if there are two max values?

Does that imply two pins high?

(try this

``````void setup()
{
for (int i = 9; i<= 12; i++)  pinMode(i, OUTPUT);
}

void loop()
{
int maxVal =-1;
int maxIndex;
for(int i=0; i<4; i++)
{
int val = analogRead(i);
if (val > maxVal)
{
maxVal = val;
maxIndex = i;
}
}

for (int i = 0; i < 4; i++)
{
if (i== maxIndex) digitalWrite(9+i, HIGH);
else digitalWrite(9 + i, LOW);
}
}
``````

yes, if there two max value, the 2 led will goes high, then if no max val, the all led is off

i will try the above coding now

robtillaart:

``````  for (int i = 0; i < 4; i++)
``````

{
if (i== maxIndex) digitalWrite(9+i, HIGH);
else digitalWrite(9 + i, LOW);
}

That won’t light two LEDs because you only have a single maxIndex. You really need to store all readings into an array:

``````int readings [4];
``````

In the process find the maximum value (like you did). Then in the second loop compare to see if the stored value is the remembered maximum one.