I'm using attachInterrupt() to catch pulses on a rotary encoder. I want to send those pulses back to my desktop application via the serial message system I'm using for this project, but it's locking up my Arduino. Here's what I've got:
volatile int rotaryPulses; //count pulses from the rotary encoder
void setup(){
Serial.begin(57600);
inputString.Reserve(400); // for incoming serial messages from desktop
attachInterrupt(0, rotaryPulseCounter, LOW);
}
void loop(){
//doing stuff...
}
void rotaryPulseCounter(){
rotaryPulses++;
Serial.print("STATUS|ROTARY PULSE: ");
Serial.println(rotaryPulses);
}
When I run the program, things function as expected, the motor turns and things are moving the way I want them to. Once the rotary encoder makes a revolution, I get the following via serial:
STATUS|ROTARY PULSE: 1
STATUS|R
...So I know that testing for LOW here is correct and it's happening when I would expect it to. However, at this point, the Arduino has locked up.
I thought maybe the problem is that by looking for the encoder's pulse to go to LOW, I might be getting "stuck" in the LOW state. I changed this to "FALLING" (also tried "RISING") and the pulse count is never incremented, even after several revolutions.
CHANGE doesn't seem like the right choice either, because I don't want to trigger the interrupt when it goes from HIGH to LOW and then again from LOW to HIGH.
Any thoughts? Am I doing something wrong here? Is the problem with Serial printing inside the interrupt routine, or is it something else?
Thanks!