Hi,
I have a rotary sensor with a reed switch, connected to an RC debounce circuit, finally connected to digital input 2 on a Duemilanove, like so:
+5V
|
12K
|
GND----o--<reed>----o-------> digital input 2
| |
--| 100nF |---
In the sketch I'm using I attach an ISR to interrupt 0, wait a certain amount of time, detach the ISR and process the results. Within the ISR pulses are counted and some other stats gathered. In the testing I've done the frequency of the pulses is estimated to go up to about 10Hz. The outline of the sketch looks like this:
volatile uint16_t num_pulses;
void
setup()
{
Serial.begin(9600);
pinMode(2, INPUT);
// Enable internal pull-up resistor
digitalWrite(2, HIGH);
}
void
loop()
{
num_pulses = 0;
// Clear any buffered interrupt 0
EIFR = bit (INTF0);
attachInterrupt(0, isr, FALLING);
delay(60000);
detachInterrupt(0);
// Use num_pulses value,
// Compute some sutff,
// Send some data over serial,
// etc
}
void
isr()
{
num_pulses++;
// Plus update some other statistics
}
The problem that I'm seeing is that with higher pulse frequencies the num_pulses counter contains wildly inaccurate values when accessed in loop() (much too high for the measurement time taken and feasible pulse rate).
One possibility is the RC debounce circuit is not adequate, but I'm also wondering about the structure of the sketch I'm using. From what I understand from Nick Gammon's pages on interrupts it would normally be necessary to protect access outside of an ISR to a value being updated in an ISR with a critical section in which interrupts are disabled, especially when a multi-byte value is updated, as I'm doing. However, the ISR here shouldn't be active when num_pulses is read, as detachInterrupt() has been called just before?