sherrellbc
sherrellbc

Reputation: 4853

Arduino - Using interrupts freezes processing and serial output?

So, the interrupts seem to work insofar as "interrupting" when an event happens. My only problem is that I the interrupts will occur 2-3 times and everything essentially stops (Serial out, everything).

I was programming the board to output serially a calculated distance based on the output of the HC-SR04 distance IC. The distances are calculated accurately but, like I said earlier, everything seems to freeze. Below is both the code and an image of the serial monitor.

enter image description here

#define TRIGPIN 4
#define ECHOPIN 3
#define RED 2
#define GREEN 13
#define INTNUM 1 //interrupt pin 1 is digital pin 3 on the duemilanove
#define PULSE 10 //microseconds 
#define CYCLETIME 50 //milliseconds

void ledWrite(int), trigPulse(), getTime();
int millisNow, millisPrev = 0;
int microsPrev;

boolean isHigh = false;


void setup() {
  Serial.begin (9600);

  pinMode(TRIGPIN, OUTPUT);
  pinMode(ECHOPIN, INPUT);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);

  attachInterrupt(INTNUM, getTime, CHANGE);
}

void loop() {
  trigPulse();
  // some other code while waiting on HC-SR04 to interrupt us when echo goes HIGH
}

void trigPulse(){
  if( (millisNow = millis()) - millisPrev >= CYCLETIME){ //sufficient cycle time 
    digitalWrite(TRIGPIN, HIGH);
    delayMicroseconds(PULSE);
    digitalWrite(TRIGPIN, LOW);
    millisPrev = millisNow; //reset clock 
  }
  return;  
}


void ledWrite(int dTime){
  int distance =  dTime/58.2;

  if (distance < 4) {  
    digitalWrite(RED,HIGH); 
    digitalWrite(GREEN,LOW);
  }
  else {
    digitalWrite(RED,LOW);
    digitalWrite(GREEN,HIGH);
  }

  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }

}

void getTime(){
  int timeNow = micros(); 
  Serial.println("Interrupted");

  if(isHigh == false){
    microsPrev = timeNow; //get time now, pin LOW->HIGH
    isHigh = true;
    Serial.println("Returning ..");
    return;
      }

  else { //pin HIGH->lOW
    ledWrite(timeNow - microsPrev);
    isHigh = false;
    microsPrev = micros();
    Serial.println("Returning ..");
    return;
  } 

  return;
}

Upvotes: 8

Views: 18295

Answers (2)

Elias
Elias

Reputation: 1427

I know this is an old thread, but I just came by it having my own problems. The problem here is that you cannot use:

Serial.Print()

Within an interrupt service routine. The reason that the Serial.Print() doesn't work within an ISR is that it uses interrupts to pull the characters out of the serial buffer, but interrupts of a certain level are masked within the ISR. What basically happens is that the arduino throws out all other interrupts that are of a lower priority, which Serial.Read() falls into.

It is documented in a number of places: link1, link2, link3

Upvotes: 16

praks411
praks411

Reputation: 1992

I think you are getting interrupt while you are already processing interrupt. You should try disabling the interrupt as soon as you are in interrupt function and re-enable it again when you are done processing just before return. Hence I would advice to have just one return so that you don't have to repeat code of enabling interrupt. Also make sure the function which you are calling inside your interrupt code do not re-enable the interrupt. It may happen that the function micros() or any of the Serial function are re-enabling the interrupt. I would suggest instead of calling function directly in you interrupt code try using some flags and set/reset in interrupt and use these flags in main loop to call your regular function.

Upvotes: 6

Related Questions