I have an encoder that fires ~54000 interruptions per second. This is way too fast for my arduino uno. But I'm trying to see what I can get from it anyway. For science.
unsigned long tickACount = 0;
unsigned long tickBCount = 0;
void myInterrupt(){
tickACount++;
tickBCount++;
}
Then in my main loop, every second I print tickACount
and tickBCount
.
I can see my arduino is able to handle only about 2050 interrupts per second. No surprise here.
What surprises me, is that tickACount
and tickBCount
have different values! tickBCount
is consistently higher that tickACount
.
Ok now I switch them to volatile. And according to the documentation, volatile only makes sense for 8bit values. The counts will eventually overflow but it doesn't really matter. I just want to see if those two variables are still different.
volatile uint8 tickACount = 0;
volatile uint8 tickBCount = 0;
void myInterrupt(){
tickACount++;
tickBCount++;
}
The result is that the two values are still different.
Also, wrapping them into some ATOMIC
macro does not help.
From what I read, interrupts cannot be interrupted.
And beside, if my interrupt was aborted midway, then tickACount
would be higher than tickBCount
, not the other way around.
Therefore, what can be the explanation for those two variables to be different?
Thank you for any idea!
tickCountB
>tickCountA
. The possibility oftickCountB
being incremented during the 2 prints hasn't come to my mind. I guess that's experience!