Hey, I wrote the tdoa code with three interrupts but the program goes the interrupts succesfully and exit. The value of rightT and leftT is written succesfuly but directT is not written and the program is frozen. When I changed the line of leftT and directT, the left is not written on the screen. I did not solve that. Please help. here is the my code.
int D = 18,R = 20,L = 19,Rp=5,Rn=6,Lp=10,Ln=11;
volatile int done0=0,done1=0, s=0; //ISR variables
volatile unsigned long leftT=0,rightT=0,directT=0; // " "
void recordL(); //ISR 1
void recordR(); //ISR 2
void recordD();
#define RPWM 150
#define LPWM 127
void Mdelay(int);
void setup() {
Serial.begin(250000);
Serial.println("Start");
pinMode(L,INPUT);
pinMode(R,INPUT);
pinMode(D,INPUT);
pinMode(Lp,OUTPUT);
pinMode(Rp,OUTPUT);
pinMode(Ln,OUTPUT);
pinMode(Rn,OUTPUT);
attachInterrupt(digitalPinToInterrupt(L),recordL,RISING); //Enable Interrupt 0 for left mic
attachInterrupt(digitalPinToInterrupt(R),recordR,RISING); //Enable Interrupt 1 for right mic
attachInterrupt(digitalPinToInterrupt(D),recordD,RISING);
}
void loop() {
A:
int detected=0,dt=0,add90=0,zone=0;
unsigned long c=0;
interrupts();
s=0, done0=0,done1=0;
while(!detected)
{
if( (!c) && (done0 || done1 || s) ) c=millis();
if(c>0 && millis()-c > 20)
{
Serial.println("input discarded");
goto A;
}
if(done0 && done1 && s)
{
detected=1;
}
}
noInterrupts();
Serial.print("Left : ");
Serial.println(leftT);
Serial.print("Right : ");
Serial.println(rightT);
Serial.print("Direct : ");
Serial.println(directT);
dt = rightT - leftT;
Serial.print("dT : ");
Serial.println(dt);
if( directT < max(leftT,rightT) )
{
Serial.println("Sound from back");
add90=1;
}
else Serial.println("Sound from front");
Serial.print("Zone ");
if( abs(dt)<300 ) // Time Difference values for Zones:
{ // Front (0 deg) : Zone 1 dt=0 to 300 us
if(add90) zone=5; else zone=1; // FRcross(45 deg) : Zone 2 dt=-300 us to -800 us
} // Right (90 deg) : Zone 3 dt=-800 us & beyond
if( abs(dt)>300 && abs(dt)<800 ) // BRcross(135 deg): Zone 4 dt=-300 us to -800 us
{ // Back (180 deg): Zone 5 dt=0 to -300 us
if(dt<0) {if(add90) zone=4; else zone=2;} // BLcross(225 deg): Zone 6 dt=300 us to 800 us
else {if(add90) zone=6; else zone=8;} // Left (270 deg): Zone 7 dt=800 us & beyond
} // FLcross(315 deg): Zone 8 dt=300 us to 800 us
if( abs(dt)>800 )
{
if(dt<0) zone=3; else zone=7;
}
Serial.println(zone);
S:
switch(zone)
{
case 0:
analogWrite(Lp,0);
analogWrite(Ln,0);
analogWrite(Rp,0);
analogWrite(Rn,0);
Serial.println("STOP");
break;
case 1: analogWrite(Lp,LPWM);
analogWrite(Ln,0);
analogWrite(Rp,RPWM);
analogWrite(Rn,0);
Serial.println("ZONE1: FORWARD");
Mdelay(1000);
zone=0;
goto S;
case 2: analogWrite(Lp,LPWM);
analogWrite(Ln,0);
analogWrite(Rp,0);
analogWrite(Rn,0);
Serial.println("ZONE 2:FORWARD RIGHT ");
Mdelay(500); //FR 45 degree turn
zone=1;
goto S;
case 3: analogWrite(Lp,LPWM);
analogWrite(Ln,0);
analogWrite(Rp,0);
analogWrite(Rn,0);
Serial.println("ZONE3 :RIGHT");
Mdelay(900); //R 90 degree turn
zone=1;
goto S;
case 4: analogWrite(Lp,LPWM);
analogWrite(Ln,0);
analogWrite(Rp,0);
analogWrite(Rn,0);
delay(1200); //BR 45 degree turn
Serial.println("ZONE4 :BACKWARD RIGHT");
zone=1;
goto S;
case 5: analogWrite(Lp,0);
analogWrite(Ln,0);
analogWrite(Rp,RPWM);
analogWrite(Rn,0);
Serial.println("ZONE5 :BACK");
Mdelay(1550); //B 180 degree turn
zone=1;
goto S;
case 6: analogWrite(Lp,0);
analogWrite(Ln,0);
analogWrite(Rp,RPWM);
analogWrite(Rn,0);
Mdelay(1200); //BL45 degree turn
Serial.println("ZONE6 :BACKWARD LEFT");
zone=1;
goto S;
case 7: analogWrite(Lp,0);
analogWrite(Ln,0);
analogWrite(Rp,RPWM);
analogWrite(Rn,0);
Mdelay(900); //L 90 degree turn
Serial.println("ZONE7 :LEFT");
zone=1;
goto S;
case 8: analogWrite(Lp,0);
analogWrite(Ln,0);
analogWrite(Rp,RPWM);
analogWrite(Rn,0);
Mdelay(500); //FL45 degree turn
Serial.println("ZONE8 :FORWARD LEFT");
zone=1;
goto S;
}
delay(1000);
}
void recordL()
{
if(!done0)
{
leftT = micros();
// Serial.println("in left");
}
done0=1;
}
void recordR()
{
if(!done1)
{
rightT = micros();
//Serial.println("in right");
}
done1=1;
}
void recordD()
{
if(!s)
{
directT = micros();
// Serial.println("in direct");
}
s=1;
}
void Mdelay(int milli)
{
unsigned long Now;
Now = millis();
X: if(millis()-Now < milli) goto X;
return;
}
The program goes the interrupt succesfully.
- I search online and I remove the part of serial.println in interrupts but nothing change.
- My micophones work succesfully, too. I check all of them.
- When I wrote the value of DirectT on the consol in interrupt it will be written.