When detecting interrupts, i see rising interrupts while the value had not gone down first and vise verse... Independent from what causes the change on a digital pin, i would expect that it goes up and down equally.
situation:
A CNY70 detects the reflecting '0' on the last digit of my analog gas meter. A lm393 based comparator board converts this to a digital signal which goes (10m away) to port D2 of a Nano Avery board where a CHANGE interrupt is trapped to count the number of zeroes that pass.
The digital value starts HIGH; when a '0' passes, the analog input of the comparator drops and the digital output goes to LOW for two or more seconds. When i poll these analog and digital values at a small interval, i see that this pattern run nicely. So actually, the process is slow enough to detect it in a normal loop(), i just like to do it via interrupts because that's how it should be.
the problem:
Apart from the expected pattern of falling - rising interrupts, i also see rising interrupts when the level was high already and falling when it was low. The number of ghost rising interrupts is much higher that the number of ghost falling interrupts and often even larger that the number of real rising interrupts.
the code:
Stripped down to the minimum for showing the behavior, the sketch looks like:
volatile unsigned int countFall = 0;
volatile unsigned int countRise = 0;
volatile int lastState = HIGH;
volatile unsigned int lowAlready = 0;
volatile unsigned int highAlready = 0;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
attachInterrupt(digitalPinToInterrupt(2), pin_ISR, CHANGE);
}
void loop() {
Serial.print( countFall); Serial.print("\t");
Serial.print( countRise); Serial.print("\t");
Serial.print( lowAlready); Serial.print("\t");
Serial.print( highAlready); Serial.print("\t");
Serial.println();
delay(1000);
}
void pin_ISR() {
if (digitalRead(2) == HIGH){
countRise++;
if(lastState == HIGH){
highAlready++;
}
lastState= HIGH;
}else{
countFall++;
if(lastState == LOW){
lowAlready++;
}
lastState= LOW;
}
}
checked already:
time between real and ghost events.
A ghost interrupt sometimes comes immediately after a valid interrupt, like 28 micros. But there can also be several second or more in between. Typically, after starting the sketch when no gas is consumed, the first interrupt after some time is a ghost rising one.
analog value of the digital pin.
I may call it a 'digital value' but how clean is that? So on another port, i also measured the analog value of this. It appeared that during ghost rising interrupts, the analog value is around 980, so almost 5V. Don't know if it says much because the interrupt likely responds to something that is much faster than measuring the analog value, but clearly, the interrupt is not just fluctuating around 2.5 volts.
Does anybody have an explanation for this behavior and preferably a solution?


