I think I see what the problem is with the code I posted but lets debug it together so you can get a better idea of what is happening in the program.
Below is the sketch with some debug code added that will show how the program is manipulating the data as it runs. I think you will see that the loudest[] array never gets sorted. Do you see why it doesn't and what change you need to make to fix the program?
int leftmicPin = 0;
int rightmicPin = 1;
int centermicPin = 2;
int values[3]; // this holds readings from the three mics
int loudest[3]; // this holds indexes to the above values, after sorting, the loudest will be first (i.e. loudest[0]);
char *Positions[3] = {"Left", "Center", "Right"}; // string used to display the calculated position
void setup() {
Serial.begin(9600);
}
void readValues(){ // function to load the value array with sensor data and initialise the loudest array so it can later be sorted
for(int i=0; i < 3; i++){
values[i] = analogRead(i);
loudest[i] = i; // this stores the index to the above values, ready for sorting
}
debugPrintValues() ;
debugPrintLoudest(); // this is the order before sorting
}
void sortValues(){ // this function is supposed to order indexes in the loudest array with the loudest first, it doesn't, do you see why?
if(values[0] < values[1])
swap(0,1);
if(values[1] < values[2])
swap(1,2);
if(values[0] < values[1])
swap(0,1);
debugPrintLoudest(); // this is the order of the loudest array ofter a single pass through sort
}
void swap(int index1, int index2){ // utility function to swap the values in the values array at the given indexes
int temp;
temp = values[index1];
values[index1] = values[index2];
values[index2] = temp;
}
void processLoudest(int index){
Serial.print("the sound is coming from the ");
Serial.println(Positions[index]);
}
void processBetween(int index1, int index2){
Serial.print("the sound is coming from the ");
Serial.print(Positions[index1]);
Serial.println(Positions[index2]);
// TODO: you could add code to calculate values[index1] - values[index2] to get the relative position if required
}
void loop()
{
readValues(); //Read all the sound sensor outputs
sortValues(); // order the array of indexes with the loudest first
if(values[loudest[0]] > 500){ // only process output if at least one value is greaer than 500
if(values[loudest[1]] > 500) // check if the first and second loudest are greater than 500
processBetween(loudest[0], loudest[1]); // process the two loudest values
else
processLoudest(loudest[0]); // only one value greater than 500
}
}
void debugPrintValues(){ // show whats happening on the serial monitor
// Show the three values we collected
// this should print: "Left=x, Center=y, Right=z" where x,y&z are values previously read
for(int i=0; i < 3;i++) {
Serial.print(Positions[i]); // i is the index, Position contains an array of strings
Serial.print("=");
Serial.print(values[i]); // print the value
Serial.print(", ");
}
Serial.println();
}
void debugPrintLoudest(){ // show the array with sorted positions on the monitor
// Show the three readings in order of loudest to quitest
// This shows how the indexes are moved as they get sorted
// this should print: "Left, Center, Right" the first time, the order should change as the array gets sorted
for(int i=0; i < 3;i++) {
Serial.print(loudest[i]); // i is the index, loudest contains an index to the actual value haled in values[]
Serial.print(", ");
}
Serial.println();
}
To answer your other questions:
'for(int i=x; i _ y; i ++ or --) behavior , Does that indicate you declared the variable 'i' right there?
Yes, int i= is a declaration of the variable i that has the benefit of only being visible within the scope of the enclosing curly brackets {} of the for loop. This construct only works in source files compiled as C++ such as the main sketch file.
Serial.print("LDR Reads: ");
Serial.println(value);
Serial.print can display strings, and numbers in various bases but it can only do one of those things in each call. So to display a string of characters followed by a number we need to call it twice. The second call to println sends the end of line character as well so the next print will be on another line. There are more examples of this usage in the debug functions just added
what do you mean by 'the if statements are cumbersome? How should they be any different?
The first versions of the sketch (before introducing the sorting function) would have required if statements detecting the six possible cases of loudness. That's a lot 'if else if' stuff and I thought that the sorting solution would be easier to follow. But the sketch can certainly be implemented using just if statements and don't let me stop you if you are more comfortable doing it that way.
Anyway, run the sketch with the debugging code and let me know if you have difficulty seeing what change you need to make to get it to work. Feel free to add any of your own Serial.print statements to further clarify what the program is doing.