Hello all,
I've been learning quite a bit about interrupts at the register level. I have noticed one thing I haven't been able to find any reports of: apparently if you have a Serial.println() statement in loop it adversely affects the operation of the interrupts (at least the external interrupts.) Below is a small bit of code I lifted from the web. It works fine if the Serial.println statement in loop is commented out, but if I include it, once I press the pushbutton connected to digital I/O pin 2 (int0) the program crashes/hangs. I don't think it has to do with switch bounce but I can't say that with 100% confidence.
Can anyone verify this or let me know if I'm doing something wrong.
Also, I apologize in advance, but my "Correct Answer" and "Helpful Answer" buttons don't seem to show up all the time, so if you do provide an answer, I may not be able to acknowledge it.
Here's the code:
#include <avr/interrupt.h>
int switchpin = 2; //pushbutton connected to pin 2 (with pullup)
void setup(){
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(switchpin, INPUT); //int0
digitalWrite(switchpin, HIGH); //enable pullup resistor
sei();
//set up for external interrupt on int0
EICRA = 0x03; //interrupt on rising edge of input to int0
EIFR = 0x01; //clear interrupt flag
EIMSK = 0x01; //enable interrupts
}
void loop(){
//Serial.println("loop");
digitalWrite(12, !digitalRead(12));
}
ISR(INT0_vect){
Serial.println(random(500));
digitalWrite(13, !digitalRead(13));
}