Arduino - Using interrupts freezes processing and serial output?

Thus, interrupts seem to work, since an “interrupt” occurs in the event of an event. My only problem is that I interruptions will occur 2-3 times, and everything essentially stops (TV series, everything).

I programmed the board to sequentially output the calculated distance based on the IC IC-SR04 output. Distances are calculated accurately, but, as I said, everything seems to freeze. Below is the code and 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; } 
+4
source share
2 answers

I think you get an interrupt when you are already handling the interrupt. You should try to turn off the interrupt as soon as you are in the interrupt function, and turn it on again when you finish processing immediately before returning. Therefore, I would advise having only one return so that you do not have to repeat the enable interrupt code. Also, make sure that the function you call inside your interrupt code does not activate the interrupt. It may happen that the micros () function or any of the sequential functions re-activate the interrupt. I would suggest that instead of calling the function directly in you, the interrupt code try to use some flags and set / reset to the interrupt and use these flags in the main loop to call your regular function.

+4
source

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

 Serial.Print() 

As part of the interrupt service routine. The reason Serial.Print() does not work in ISRs is because it uses interrupts to draw characters from the serial buffer, but interrupts of a certain level are masked in ISRs. What basically happens is that arduino throws all other interrupts that have a lower priority, which Serial.Read () refers to.

It is documented in several places: link1 , link2 , link3

+11
source

Source: https://habr.com/ru/post/1494633/


All Articles