I'm trying to get the arduino mega to read multiple (!) S0 power meter outputs by external interrupts (2 and 3, pin 21 and 20 at the moment - will become more) and display the counters on lcd and print to serial.

In my code I attached the interrupts to the pins and activated the pull-up resistors. My (copied) code basically seems to work ok, I did no debouncing in the interrupt routine as to not artificially lengthen the time wasted in the interrupt routine.

Now worse case would be 2 pulses at exactly the same time, although iirc the arduino does some interrupt queuing. So I connected both pins and as a test connect them to ground. Pulses are counted (remember no debouncing), but both counters differ quite a lot (36 to 48 e.g.).

/*The code below detects the falling edge of each pulse and increment pulseCountIt calculated the power by the calculating the time elapsed between pulses.Read more about pulse counting here:http://openenergymonitor.org/emon/buildingblocks/introduction-to-pulse-counting

unsigned long pulseCount3 = 0;unsigned long pulseCount2 = 0;unsigned long pulseTime,lastTime; // Used to measure time between pulsesdouble power;int ppwh = 1; // pulses per watt hour - found or set on the meter.

Any time variables are going to be shared by different functions in a sketch ( like in loop() and inside ISR functions ) you must declare them 'volatile' so that the compiler will not store them into registers for faster access, but rather be forced to get them from SRAM when reading or writing to them. Failure to do so can lead to very strange and improper operation of the code.

As those variables are 4 bytes long it takes several machine instruction cycles to read them from SRAM and it's quite possible an interrupt ISR can be triggered in between the reading of each byte and therefore the value can be changed and thus invalid as read by the loop() function. Typical protection from this behavior is to either bracket the reference to the variable in the loop function like this: