Arduino Duemilanove crashes

I have created a program to perform a 10 point moving average on to analog inputs (microphones), and then take the difference of them to give the direction of sound.

The Arduino runs for around a minute and then crashes, here is the code;

int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int micPin1 = 0;
int micPin2 = 1;
const int numReadings = 10;
int mic1[numReadings];
int mic2[numReadings];
int mic1_total = 0;
int mic2_total = 0;
int mic1_avg = 0;
int mic2_avg = 0;
int index = 0;
int dif = 0;

void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(micPin1, INPUT);
pinMode(micPin2, INPUT);
analogReference(EXTERNAL);
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
{
mic1[thisReading] = 0;
}

}
void loop()
{
mic1_total = mic1_total - mic1[index];
mic1[index] = analogRead(micPin1);
mic1_total = mic1_total + mic1[index];

mic2_total = mic2_total - mic2[index];
mic2[index] = analogRead(micPin2);
mic2_total = mic2_total + mic2[index];

index = index + 1;

if (index >= numReadings)
{
index = 0;
mic1_avg = mic1_total / numReadings;
mic2_avg = mic2_total / (numReadings);
Serial.print (mic1_avg);
Serial.print ("\t");
Serial.print (mic2_avg);
Serial.println ();
}

dif = mic1_avg - mic2_avg;

if (abs(dif) > 100)
{
if (dif > 0)
{
sound_forward();
}

if (0 > dif)
{
sound_left();
}
}
else
{
sound_error();
}
}

//Sub-Routines

void sound_forward() //Fast Forward
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, HIGH);
delay(100);
loop();
}

void sound_left() //Slow Left
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
delay(100);
loop();
}

void sound_error()
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
delay(100);
loop();
}

Any ideas?

Thanks

Mark

Calling loop from inside a function that is called from loop is not really a good idea. That results in a recursive call, which results in a new set of variables, etc. Eventually, you are running out of memory, resulting in some memory getting overwritten, which confuses the heck out of the Arduino.

Why are you calling loop directly?

Not sure to be honest, i've deleted them now and its working fine. My next challenge is to interface a third and fourth mic, and again decide the direction. I think the easiest method will be to take the moving average and store it in an array and perform a sort to determine the highest two values and then take the difference again and perform the same if positive then one direction if negative then the other direction. the only problem is once sorted the arduino wont know which value was from which mic.

Is this the correct way to go about this, or is there an easier way?

Mark