Same value for my time difference in my sound localization project

This project involves tracking the position of the source of sound via the angle. I plan to add a pointer later on controlled by a servo-motor. I am using two arduino microphones with a digital output pin. The two time variables, t1 and t2, may vary but the difference between them always remains either 8 or 4. Is the problem resulting from my code or should I change my microphones. Also if my microphones are the problem, kindly suggest better ones for this project. I have posted the code below.

#define MICROPHONE_1_PIN 7 // Digital pin for microphone 1
#define MICROPHONE_2_PIN 8 // Digital pin for microphone 2

#define SPEED_OF_SOUND 343 // Speed of sound in m/s at room temperature
#define MIC_DISTANCE 0.15 //15cm distance between the microphones
void setup() {
  Serial.begin(9600);
  pinMode(MICROPHONE_1_PIN, INPUT);
  pinMode(MICROPHONE_2_PIN, INPUT);
}
void loop() {
  unsigned long t1 = 0, t2 = 0;
  unsigned long startTime=micros();

  // Wait for  microphone 1 to detect sound
  while (digitalRead(MICROPHONE_1_PIN)==LOW){

    delayMicroseconds(100);// Delay to control the sampling rate
  }
  // Record time when microphone 1 detects sound
  t1 = micros()-startTime;
  
  // Wait for microphone 2 to detect sound
  while (digitalRead(MICROPHONE_2_PIN)==LOW){

    delayMicroseconds(100);
  }
  
  // Record time when microphone 2 detects sound
  t2 = micros()-startTime;
  
  // Calculate time difference of arrival (TDOA) in microseconds
  unsigned long tdoa =abs(t2 - t1);
  
    Serial.println(tdoa);
  
  // Convert TDOA to distance in meters
  float distance = tdoa / 1000000.0 * SPEED_OF_SOUND;
  
  // Calculating angle of arrival based on distance and spacing between microphones
  float angle = acos(distance / 0.15) * 180 / PI; // Spacing between microphones is 0.15 meters (15cm)
  
  // Output the angle of arrival
  Serial.print("Angle of Arrival: ");
  Serial.println(angle);
  
  delay(1000); 
}

Hi @greatme54

welcome to the arduino-forum.

well done posting your code as a code-section (or almost a single code-section)

From your description I do not yet understand how this works.
You have two microphones with a distance of 15 cm between the microphones

Within 100 microseconds sound travels
343 m * (100 / 1000000) = 0.0343 m = 3.43 cm

So in case the sound comes from an angle of 0° to the microphones
Mic1
.
.
.
Mic2
.
.
.
.
.
.
Source of sound

the time-difference between Mic1 and Mic2 is
15 cm / 3.43 cm/100µSecs = 4.3 iterations of your while-loop

If the angle between the source of sound and your microphones becomes bigger
the time-difference becomes smaller.

if the angle is 90° there is no time-difference
Mic1
.
............................................................................................................Source of sound
.
Mic2

The function-calls to digitalRead() require some time too.
You should measure how many microseconds this is.

Perhaps the microphones are triggering in response to background noise, rather than the same sound wavefront.

digitalRead() takes a couple of microseconds, so it is not particularly significant compared to the 100 us delay (sound travels 3.4 cm in 100 us).

Direct port read on an Arduino Uno and the like would be perhaps 100x faster, but that is not the problem.

The microphones have an LED that lights up when they detects sound. When my triggering sound is not available, the LEDs are off which makes me believe that the microphones are not being triggered by any background noise.

Your program seems to report that they are being triggered. Please explain.

Perhaps the LED flashes so briefly that you don't notice.

You have described "sound level detection", not the detection of a "sound". What are you using to create your test sound?

I am assuming maybe the microphones are defective(I'm not 100% sure).However,when I do have my source the values of t1 and t2 change,leading me to believe that it reads the souce, but their difference does not

Sorry, I meant detection of a sound.I am snapping my fingers

I know what you mean, but that does not change the fact that your devices are reacting to level, volume, of the sound, not the structure of the sound.
To begin to make your plan work, you need to ensure the two sensors trigger at the same volume level. Might take some time to get the gain control pots set make the sensors trigger at the same sound level.

Much more likely, you are not using them properly.

Make sure that each one can be reliably triggered by the same very repeatable, short, loud sound. Snapping your fingers is not a good choice.