[SOLVED] Arduino stops when using Interrupts() - Serial freezes

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. Whichever LED is HIGH when the freeze happens is how it remains.

I thought that maybe an interrupt was occurring during an earlier interrupt call. So, I thought that by calling noInterrupt() in the functions that should not be interrupted(otherwise there would be essentially a recursive phenomena occurring) would help; it did not.

#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() {
  interrupts();
  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 < 15) {  
    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;
}

You can't use Serial.print() in an ISR because it uses interrupts.

sharing the knowledge..

LarryD:
You cannot use delays in an interrupt.

•Don't attempt to delay, eg: delay (100);

•You can get the time from a call to millis, however it won't increment, so don't attempt to delay by waiting for it to increase.

•Don't do serial prints (eg. Serial.println ("ISR entered"); )

•Don't try to do serial reading.

See:
Gammon Forum : Electronics : Microprocessors : Interrupts

UKHeliBob:
You can't use Serial.print() in an ISR because it uses interrupts.

This was exactly the problem. Thank you! Ironically, I only added those serial out lines to make sure everything was working properly!

eddiea6987:
sharing the knowledge..

LarryD:
You cannot use delays in an interrupt.

•Don't attempt to delay, eg: delay (100);

•You can get the time from a call to millis, however it won't increment, so don't attempt to delay by waiting for it to increase.

•Don't do serial prints (eg. Serial.println ("ISR entered"); )

•Don't try to do serial reading.

See:
Gammon Forum : Electronics : Microprocessors : Interrupts

Thank for the llink. Very helpful.