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.).
My code based on Openenergymonitor:
The code below detects the falling edge of each pulse and increment pulseCount
It calculated the power by the calculating the time elapsed between pulses.
Read more about pulse counting here:
Part of the openenergymonitor.org project
Licence: GNU GPL V3
Changed for my personal use
// Library for LCD
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
unsigned long pulseCount3 = 0;
unsigned long pulseCount2 = 0;
unsigned long pulseTime,lastTime; // Used to measure time between pulses
int ppwh = 1; // pulses per watt hour - found or set on the meter.
// Initialize Serial USB
// Start LCD Library
// Print title
lcd.print("SO Stromzaehler ");
// Cursor to pos1 line 2
// pinmode auf INPUT setzen
pinMode (20, INPUT);
pinMode (21, INPUT);
// Pull-up auf pin 20 aktivieren
// pulse detection interrupt (pulse channel - IRQ3 pin20)
attachInterrupt(3, onPulse3, FALLING);
attachInterrupt(2, onPulse2, FALLING);
// lastTime = pulseTime;
// pulseTime = micros();
// power = int((3600000000.0 / (pulseTime - lastTime))/ppwh); // calculate power
//Set Cursor to line 2
// Print counters on LCD
lcd.print ("P1 ");
lcd.print ("P2 ");
// Serial.print(' ');
// Serial.println(pulseCount2 * ppwh); // watt hour elapsed
// The interrupt routine - runs each time a falling edge of a pulse is detected
pulseCount3++; // count pulse
pulseCount2++; // count pulse