Hi
I am looking for a way to read say 5 sensors (A,B,C,D,E) and return the sensor with the maximum value.
That is, I don't need the value read by the sensor but which sensor ( A - E) had the greatest value.

I have looked at the "max" function but that only works with two values and returns the value not which value. I don't want to have to go through and compare A to B then A to C etc until D to E. There must be a better way.
Thanks

I don't want to have to go through and compare A to B then A to C etc until D to E.

Why not?

1,3,2,1,1.

Which sensor returned the biggest value?
How did you work that out?

``````max(A, max(B, max(C, max(D, E))))
``````

If you need to do that multiple places and you would prefer a syntax like maximum(A, B, C, D, E) then you can create that function.

pert:

``````max(A, max(B, max(C, max(D, E))))
``````

If you need to do that multiple places and you would prefer a syntax like maximum(A, B, C, D, E) then you can create that function.

No, that doesn't return the index (hint) of the sensor with the largest reading.

Ah, I see now. How’s this?

``````int sensorValue[5];
const byte sensorCount = sizeof(sensorValue) / sizeof(sensorValue[0]);
byte maxSensorIndex;
for (maxSensorIndex = 0; maxSensorIndex < sensorCount - 1; maxSensorIndex++) {
bool isMax = true;
for (byte sensorIndex = 0; sensorIndex < sensorCount; sensorIndex++) {
if (sensorValue[maxSensorIndex] < sensorValue[sensorIndex]) {
isMax = false;
break;
}
}
if (isMax == true) {
break;
}
}
``````

Should work. Nice solution.
One minor detail: I would add a

``````#define SENSORCOUNT 5[code]
and use that instead of calculating the size, and use the same macro to create the sensorValue[] array, and probably one or two more arrays related to the sensor such as the pin connection.
``````