Note that there are some explanatory texts on larger screens.

plurals
  1. POArduino - Using interrupts freezes processing and serial output?
    primarykey
    data
    text
    <p>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). </p> <p>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. </p> <p><img src="https://i.stack.imgur.com/2gQZV.png" alt="enter image description here"></p> <pre><code>#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 &gt;= 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 &lt; 4) { digitalWrite(RED,HIGH); digitalWrite(GREEN,LOW); } else { digitalWrite(RED,LOW); digitalWrite(GREEN,HIGH); } if (distance &gt;= 200 || distance &lt;= 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-&gt;HIGH isHigh = true; Serial.println("Returning .."); return; } else { //pin HIGH-&gt;lOW ledWrite(timeNow - microsPrev); isHigh = false; microsPrev = micros(); Serial.println("Returning .."); return; } return; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload