jremington:
You probably can't expect much useful help on this forum unless you take more time to describe the actual experiment. Describe the sound, the microphone and associated circuitry and what you have done so far (including code) to determine whether you are actually detecting the sound of interest.
#include <FreqCount.h>
#include <math.h>
volatile unsigned long firstPulseTime;
volatile unsigned long lastPulseTime;
volatile unsigned long numPulses;
unsigned long currentTime = 0;
unsigned long pastTime = 0;
unsigned long value;
double x;
double y;
float ab;
double w;
double a,b,c,d,e,f,g,h,k;
double xb;
unsigned long timePassed, timePassed2, timePassed1;
int servo = 8;
int angle;
int pwm;
void isr()
{
unsigned long now = micros();
if (numPulses == 1)
{
firstPulseTime = now;
}
else
{
lastPulseTime = now;
}
++numPulses;
}
void setup() {
xb = 5.65;
x = 100;
FreqCount.begin(1000);
pinMode(3, OUTPUT);
analogWrite(3, 128);
pinMode(servo, OUTPUT);
Serial.begin(57600);
}
float readFrequency(unsigned int sampleTime)
{
numPulses = 0;
attachInterrupt(0, isr, RISING);
delay(sampleTime);
detachInterrupt(0);
return (numPulses < 3) ? 0 : (1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime);
}
void servoPulse (int servo, int angle)
{
pwm = (angle*11) + 500;
digitalWrite(servo, HIGH);
delayMicroseconds(pwm);
digitalWrite(servo, LOW);
delay(50);
}
void loop() {
if (FreqCount.available()) {
unsigned long count = FreqCount.read();
float freq = readFrequency(1000);
if (freq == 0 && count==0){
pastTime = currentTime;
currentTime = micros();
if (freq > 0 && count == 0){
timePassed1 = (currentTime - pastTime - 1000000);
}
else if (count > 0 && freq == 0 ){
timePassed2 = (currentTime - pastTime - 1000000);
}
timePassed = timePassed2 - timePassed1;
if(timePassed > 0){
ab = (343.2*timePassed/10000);
y = xb * xb;
w = ab * ab;
a = x * x;
b = w/4;
c = 4*y;
d = c/w;
e = d - 1;
f = a*e;
g = sqrt (b - y + f);
h = atan2 (g, x);
k = h*180/PI;
for (angle = 180; angle >= (k); angle +=5) {
servoPulse(servo, angle); }
}
else if (timePassed < 0){
timePassed = timePassed1 - timePassed2;
ab = (343.2*timePassed/10000);
y = xb * xb;
w = ab * ab;
a = x * x;
b = w/4;
c = 4*y;
d = c/w;
e = d - 1;
f = a*e;
g = sqrt (b - y + f);
h = atan2 (g, x);
k = h*180/PI;
for (angle = 0; angle <= (180 - k); angle +=5) {
servoPulse(servo, angle); }
}
}
delay(1000);
freq = 0;
count = 0;
}
}
This is the code. What I'm trying to achieve here is to reset the measurement as zero at the beginning of the loop and then obtain the time difference by starting the time count when one of the mics are high and stop when the other one becomes high, too. It may sound too basic (it doesn't work anyway) and the logic behind may be wrong.
The sound I'm dealing with is continuous not impulse, which makes everything much more complex. The letter variables belong to the TDOA algorithm that is supposed to give the result as an angle if the time difference is found at the start. My intention is to direct the servo to the source of the sound by using that result and also still keep measuring the frequency.
The mics I'm using are MEMS microphone boards(link of the datasheet https://www.sparkfun.com/datasheets/Components/General/SPM0404HE5H.pdf), so no additional gain amplifiers are included to the circuit. The circuit is a simple one, containing two microphones only, not even a single resistor or an opamp. I'm using Arduino Due. But I have an extra Arduino Leo if number of timers or anything else would be insufficient when Due used.
That's the case. Please help on this issue. My graduation completely depends on that. I can give more information about the project if needed.