I've got a strange issue with a LM393 which is hooked up to one of the interrupt pins of a Mega 2560. The LM393 is outputting a logical 1 or 0 depending if the CNY70 on the input detects something, where the 1 is supposed to trigger an interrupt.
When the LM393 outputs a logical 1, the interrupt routine (set as RISING) is triggered which only does one thing: setting a volatile variable HIGH. Back in the main loop, an if-statement is called (which can only be called with that variable set as HIGH) which does some calculating and outputs the result to Serial3 which is hooked up to a HC-06 bluetooth module. At the end of the if-statement, the variable is set as LOW after which the Arduino is waiting for another interrupt.
The interrupt routine is called when it's supposed to, but unfortunately there's something going wrong. The output is supposed to be a 1 to 4 digit number depending on the amount of time between interrupts. That number appears. (although it's hard to check if it's 100% accurate), but after each interrupt, that number is followed by a bunch of zero's (each on a new line) and -4480 appears a couple of times as well. Then it goes quiet on Serial3, till the next interrupt comes and then the same thing happens again: expected value, followed by a bunch of zero's and -4480.
At first I thought the sensor and LM393 were generating some false positives directly after the pulse that's supposed to be generated. But after connecting the LM393 to a regular digital pin (set as input), I only saw ones and zero's passing by when they were supposed to, I didn't see any false positives.
I also tried setting up the internal pull down (and later pull up) resistor on that interrupt pin, didn't change anything. Also checked for shorts, didn't find any.
And now I'm at a loss, does anyone have any ideas what this could cause?
Sketch:
#define ms_per_hour 3.6e6
#define cycles_per_kwh 375
int W = 0;
long starttime;
long duration;
volatile int wattpulse = LOW;
void setup(){
// open serial port 3:
Serial3.begin(9600);
attachInterrupt(0, watt, RISING);
starttime = millis();
}
void loop() {
if (wattpulse == HIGH) {
duration = millis() - starttime;
starttime = millis();
W = ms_per_hour / ((cycles_per_kwh * duration) / 1000); // calculate current power usage
Serial3.println(W);
wattpulse = LOW;
}
}
void watt() {
wattpulse = HIGH;
}
edit: And it just got weirder:
After using a 9v power supply instead of an USB-power supply, the output cleared up, just a little.
Now I'm getting the expected value when the output of the LM393 becomes 1, then the zeros again, and then another value precisely when the output of the LM393 becomes zero, followed by some zeros.
It looks like the interrupt is triggered not only on rising, but also on falling, while it's set to trigger only on rising. That, and the zeros are still there, which I can't explain.
edit2: After some more Googling, I put together a RC-filter that has cleared up most zeros (guessing debouncing was needed after all) but not all of them, plus the falling-edge still triggers an interrupt.