# Maximum Value of Array

I am working on a project using an MSGEQ7 chip for music frequency analysis. The output of this chip is a 7-value array for every cycle, with each part of the array being a value for intensity of a frequency band.
What I am trying to do is find the maximum value of the array each time, to then get the frequency band with the highest intensity and use that to specify an output to LED’s.
The output I have is again a 7 value array, and I want to get the maximum value for that array. I cannot find anything that will combine more than 2 variables easily, so I am turning here for help.
I attached the part of my code that is dealing with the array, and this is where I need to pull the max value from.

Thanks

``````for (int i = 0; i < 7; i++)
{    digitalWrite(strobePin, LOW);
delayMicroseconds(30); //alllow output to settle
Serial.print(spectrumValue[i]);
Serial.print(" ");
digitalWrite(strobePin, HIGH);
}
``````

You want to do something like this.

``````unsigned kmax=0; max=0;
for (byte k=0; k<7; k++)
if (spectrumValue[k] > max) {
max = spectrumValue[k];
kmax = k;
}
//  max now contains the largest spectral value.
// kmax contains the index to the largest spectral value.
``````

Thanks a lot, I added your section in and it looks like it is working!

``````for (int i = 0; i < 7; i++)
{    digitalWrite(strobePin, LOW);
delayMicroseconds(30);
Serial.print(spectrumValue[i]);
Serial.print(" ");
digitalWrite(strobePin, HIGH);
for (byte k=0; k<7; k++)
if (spectrumValue[k] >specmax)
{    specmax = spectrumValue[k];
kmax = k;   }
}
if (kmax == 0)
leds = CRGB::Red;
else if (kmax==1)
leds = CRGB::MediumBlue;
else if (kmax==2)
leds = CRGB::Green;
else if (kmax==4)
leds = CRGB::Orange;
else if (kmax==6)
leds = CRGB::Magenta;
``````

stuarts code is good enough for what youre doing but you should think about what can happen if the contents of spectrumValue are all negative. the initial value of zero for 'max' would yield erroneous results.

wg0z:
stuarts code is good enough for what youre doing but you should think about what can happen if the contents of spectrumValue are all negative. the initial value of zero for 'max' would yield erroneous results.

Yes I should have mentioned that this assumes that the "spectrumValues" are unsigned or always non negative. They come from Analogread (0..1023) in the op's example code.

jah8918:
Thanks a lot, I added your section in and it looks like it is working!

To be honest I'm surprised that is working the way it's inserted. You don't need to do my loop inside your existing loop.

You can do the two loops separately if you want, with the scan for maximum loop coming after the analog read loop. Or you can just do the whole thing, read and scan, in the one loop. But please drop the nested loops there.