I have this code that :
1)reads all 16 analog inputs of an arduino Mega.
2) Then it puts the values in an 2d array ( the seccond row is then so i can keep a track of the index).
Then try to sort the array and get the 9 maximum values sorted in an other 2d array MaxValues[][] by keeping the index. It doesn't . all it does is filling all 9 spaces with the bigest value and the index of it.
this is the code that I am using :
int gauge[2][16] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
};
int MaxValues[2][9] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8}
};
int f = 0; //flag value 1
int flag = 0; // flag value 2
void setup()
{
Serial.begin(9600);
}
void loop() {
for (int i = 0; i <= 15; i++) { // begin and read all 16 analog and place then in the first row of gauge[][] aray
gauge[0][i] = analogRead(i);
}
// for (int j = 0; j <= 15; j++) {
// Serial.print(gauge[0][j]);
// if (j < 15) {
// Serial.print(",");
// } else {
// Serial.println(",");
// }
// delay(10);
// }
for (int k = 0; k <= 15; k++) {
for (int l = 0; l <= 15; l++) {
f = 15;
for (int x = 0; x <=8; x++) {
if (gauge[0][l] > MaxValues[0][x]) { // if the value is biger than MaxValue[][] you have to start moving all values one place next and then place the value that was biger at the opened space in MaxValues[][] array
if ( f == 15 ) {
flag = 15;
}
for (int m = 8; m > x; m--) {
if (flag == 15) {
MaxValues[0][m] = MaxValues[0][m - 1];
MaxValues[1][m] = MaxValues[1][m - 1];
MaxValues[0][x] = gauge[0][l];
MaxValues[1][x] = gauge[1][l];
flag = 0;
f = 0;
}
}
}
}
}
}
for (int row = 0; row <=1; row++)
{
for (int col = 0; col <=8 ; col++)
{
Serial.print(MaxValues[row][col]);
Serial.print("\t");
}
Serial.println();
}
}
All i am tryng to do is keep track and sorted witch 9 sensors out of my 16 had the biggest values.
any ideas are more than welcome.