Arduino - Using interrupts freezes processing and serial output? -


so, interrupts seem work insofar "interrupting" when event happens. problem interrupts occur 2-3 times , stops (serial out, everything).

i programming board output serially calculated distance based on output of hc-sr04 distance ic. distances calculated accurately but, said earlier, seems freeze. below both code , image of serial monitor.

enter image description here

#define trigpin 4 #define echopin 3 #define red 2 #define green 13 #define intnum 1 //interrupt pin 1 digital pin 3 on 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();   // other code while waiting on hc-sr04 interrupt 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; } 

i think getting interrupt while processing interrupt. should try disabling interrupt in interrupt function , re-enable again when done processing before return. hence advice have 1 return don't have repeat code of enabling interrupt. make sure function calling inside interrupt code not re-enable interrupt. may happen function micros() or of serial function re-enabling interrupt. suggest instead of calling function directly in interrupt code try using flags , set/reset in interrupt , use these flags in main loop call regular function.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -