Hi, are there any known problems with attachInterrupt?
I think I may have found one while working on my weather station project, but I know I need to demonstrate it in the simplest possible way, so that there is no doubt (in my head or anyone else’s) that the problem really exists. I’ve run out of time to do that tonight, so I thought I’d just ask the question and work on it tomorrow evening.
The problem may be:
attachInterrupt(rainGuagePin, rainGuageEvent, RISING);
seems to behave exactly the same as:
attachInterrupt(rainGuagePin, rainGuageEvent, CHANGE);
in that the interrupt seems to be getting triggered on both rising and falling edges, when I only want rising.
I have a rain guage attached to pin D3. The rain guage contains a “see-saw” mechanism which causes a magnet to sweep past a reed switch. My interrupt function contains code to debounce the sensor:
volatile unsigned long rainEvents = 0;
volatile unsigned long nextRainEvent = 0;
void rainGuageEvent() {
unsigned long m = millis();
if (m > nextRainEvent) {
rainEvents++;
nextRainEvent = m + 10;
}
}
...
pinMode(rainGuagePin, INPUT_PULLUP);
attachInterrupt(rainGuagePin, rainGuageEvent, RISING);
Each time the rain guage “tips”, it should cause 1 rising and 1 falling edge, as long as my debounce code is working. So rainEvents should get incremented by 1 each time the see-saw tips. But it increments by 2. If I change “RISING” to “CHANGE”, it behaves exactly the same.
Tomorrow I will try to demonstrate the issue with the simplest possible circuit and code I can devise. I will attach a digital pin (set as OUTPUT) to another digital pin (set as an INPUT, with interrupt attached). I can then write a very small program to toggle the output pin and see how many times the interrupt is triggered.
If I’m making a fool of myself, could someone please point this out before I waste time chasing any wild geese!
Paul