Comparing Values in an Array

Without bruit force comparing each individual element in an array, how would one compare each element to see if they're all the same? I'd like to throw a switch when all of the values stored in an array are the same, no matter what they are. The purpose of this is to show when an ADC has stabilized in any given state.

Thank for the input :slight_smile:

Would something like this work?:

all_equal_flag = 1; //0 means not all elements are equal, 1 means all elements are equal

//Go through array and set "all_equal_flag" to zero if an inconsistency is found.
//Start from 1 so that the first 2 elements are compared instead of element[-1]

for(int i = 1; i < arraySize; i++){
  if(some_array[i] != some_array[i-1]){
    all_equal_flag = 0;

a simple loop iteration would be the easiest way though...

Without bruit force comparing each individual element in an array

That is the ONLY way of doing it. You may in some other languages have a constrct like if ( Array1 == Array2) or such like, but that is just a language feature, to save on typing. Behind the scenes the processor can only compare two numbers at one time. In other words, you have to do something like

// (untested code)
for ( n=1; n<ARRAYLENGTH; n++) 
  if ( array1[n] != array1[0] ) break ;
// if ( n> ARRAYLENGTH ) all elements where the same.

You will probably never get all elements the same as the ADC has a +/- 1bit "noise/random" error, so if they are +/- 2 of the same value, it is steady state. Maybe even +/- 10 or 25 to account for noise variations in your sensor. I usually average a few measuring points.

Cool, thanks. I can live with a bit of adc error, which is easy to write into the if-statement. Your code is a little cleaner than mine since it ends sooner if the condition is met. We're not talking a big array, so "steady state" is going to be maybe 10 or 20 samples.

Bruit force, it is.

If you're only waiting for the input to settle then building up the whole array and brute-force comparing every element does not feel like an elegant solution. Wouldn't it be better to maintain a rolling average and stop when the average stops changing, or stop when the number of samples 'close enough' to the average exceeds some value, or something like that? That would require less data and give you the option to increase the number of samples you're analysing, without any significant increase in code complexity.

That could work as well, with similar results. I implemented the above code suggested by Msquare with my own tweaks, giving it a tolerance of ±15 bits. My system is VERY low noise and the ADC is stable at 1 bit error. It turns out that it stabilizes in about 2 or 3 samples, so I can read 3 samples within my tolerance and save that reading. It only takes 6 lines of code to do this and keep a rolling array going.

This will definitely be more useful in a noisy environment, but for me it only takes 3 samples to settle down. I didn't want to take an average I wanted a target center with defined tolerances.