I am using an interrupt pin with a mechanical, 4-bit rotary encoder. The interrupt is monitoring the first pin of the encoder to detect when there is a CHANGE to trigger the ISR. The ISR updates an LCD display.
The issue is, when I was first messing with this, I was getting odd behavior where the interrupt would trigger multiple times in quick succession, so I figured it was switch debouncing issues. After scoping it, it definitely was. So a hardware, low-pass filter was added and the signal is now smooth as silk. The interrupt fires once, and only once, during each "click" of the encoder.
The part that has me scratching my head is this, the interrupt only works correctly when I have delay(1000) inside of the ISR. But that's strange because delay() isn't even supposed to work inside an ISR! The ISR certainly doesn't delay for 1000 ms, or I would surely notice it in the refresh on the LCD.
But when I removed delay(1000) it acts erratic. Here's what erratic means: the encoder is 4 bits (16 positions, 0 to 15). When incrementing the encoder is should count 0 to 15 going up and 15 to 0 going down, but without the delay() (and it can be delay(1000), delay(500), or delay(10), it all still works the same) the ISR doesn't work and the encoder count jumps around. Here is output from the two program variations: "Dec Pos" is just the position translated from 4-bit binary to a decimal position value
WITH delay() //Notice the numbers are sequential both up and down. I increment until 15 and then decrement until 4
Dec Pos: 10
Dec Pos: 11
Dec Pos: 12
Dec Pos: 13
Dec Pos: 14
Dec Pos: 15
Dec Pos: 14
Dec Pos: 13
Dec Pos: 12
Dec Pos: 11
Dec Pos: 10
Dec Pos: 9
Dec Pos: 8
Dec Pos: 7
Dec Pos: 6
Dec Pos: 5
Dec Pos: 4
WITHOUT delay() //Notice the numbers jump around. I increment until 15 and then decrement. But it's hard to know what the real position is.
Dec Pos: 8
Dec Pos: 9
Dec Pos: 10
Dec Pos: 11
Dec Pos: 14
Dec Pos: 15
Dec Pos: 10
Dec Pos: 11
Dec Pos: 8
Dec Pos: 15
Dec Pos: 6
Dec Pos: 7
Dec Pos: 4
Dec Pos: 5
I have attached 2 sketches to this post. One with the delay() and one without. Only the delay() work correctly. Any thoughts?
I read here that potentially some interrupts can't handle a ChangeIntterupt, but I am not sure if I interpreted that correctly. Problem with MEGA2560 and interrupts - Programming Questions - Arduino Forum
I have tried pins 2 and 3 as the interrupts.
Ionic_v01_noDelay.ino (2.31 KB)
Ionic_v01.ino (2.47 KB)