Go Down

Topic: the serial port is frozen, when back to interrupt (Read 203 times) previous topic - next topic

ramcols

Mar 10, 2017, 11:04 pm Last Edit: Mar 11, 2017, 01:52 am by ramcols Reason: Format
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.


Code: [Select]
  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.

1. I search online and I remove the part of serial.println in interrupts but nothing change.
2. My micophones work succesfully, too. I check all of them.
3. When I wrote the value of DirectT on the consol in interrupt it will be written.


pert

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

ramcols

#2
Mar 11, 2017, 01:24 am Last Edit: Mar 11, 2017, 01:53 am by ramcols Reason: Format
I am sorry i fixed it.
I promise next time i will care rules of forum.
It looks so good.

PaulS

Why are you turning interrupts off before calling functions that need interrupts enabled?

Why are you diddling with interrupts at all?
The art of getting good answers lies in asking good questions.

ramcols

Because after the took the voice from outside starts to calculate of angle and while of this process new sound is not accepted avoiding from confusion

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy