Reading 2 pulses with 2 interupts

I am trying to read two pulses supplied from electicity meters. then send the resuts out to an LCD & XBEE
transmitter, then read by a remote pc.

When try to add the second interupt it seems to clash with the first given random results.

I am new to programming so would be pleased to receive some help.

LCD_Combind_biult_in_lcd_two_input_test.ino (1.07 KB)

// KWH interrupt attached to IRQ 1  = pin21 
attachInterrupt(2, onPulse, FALLING);
Serial.begin(4800);
attachInterrupt(1, onPulse, FALLING);//pin 3

Which Arduino do you have?

Why are you calling the same function for both interrupts?

delay(1000);

In an ISR? Dream on.

Using the Mega 2560.

I not sure how to do right, I thought 2 interupts with 2 definitions would do it.

I not sure how to do right, I thought 2 interupts with 2 definitions would do it.

You have two interrupts and one handler. The handler is flawed, but that may be beside the point.

What, exactly, are you trying to read? It sounds like you have two meters generating pulses as electricity is used. Don't you want to keep the usage separate?

You need, in your handler(s), to simply count each pulse. You do something, in loop(), with the count(s). You do NOT delay() in an ISR. You do not Serial.print() in an ISR.

Reading pulses from meters flashing leds fed into the arduino I have 2 meters and and would like to see both on the lcd and send serial data from both it does work on 1 pulse adding the second one is above my knowledge.

I did try delay but it effects the accuracy. do you think you could write someting for me?

If you can't see that you need two separate interrupt routines (or none at all if done differently) then there is probably no hope for you.

do you think you could write someting for me?

How much are you willing to pay ?

How much do you want?

Get one interrupt service routine counting pulses from one meter. Call that function countPulses1().

Then, it should be trivial to see what needs to be done to count pulses from meter 2.

Thanks Paul. I have been on it 3 days now, I'm 68 so no longer the sharpest tool in the box. I think it over my head thats why I decided to post.

You said

When try to add the second interupt it seems to clash with the first given random results.

To me that implies that the first one works. For the reasons given (printing and delay()ing in the ISR) I doubt that, but what exactly does happen when you run the code with only one interrupt active ?

If your program is only intended to read 2 inputs, increment counters when one or other goes HIGH and display the results of a calculation based on the counts then you probably don't need to use interrupts in the first place which would simplify the code requirements somewhat.

I have it up and running on a single meter read, I decided to add a second arduino for the second meter, thinking further I thought I could do it on one arduino which is where I am now.

Can you please post your code that works with one meter and one interrupt ?

Read up on interrupts and timers, then come back. The information you seek is on these two pages.

paulepc:
I'm 68 so no longer the sharpest tool in the box.

Your age has nothing got to with it unless you allow yourself to think you are too old.

You have beeen given a lot of useful information already. Just take time to consider it. I find things become clearer after the 12th reading.

...R

UKHeliBob:
Can you please post your code that works with one meter and one interrupt ?

Here is a working one interupt.

#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
long pulseCount = 0;
unsigned long pulseTime,lastTime;
double power, elapsedkWh;
int ppwh = 1; //1000 pulses/kwh = 1 pulse per wh
void setup(){
lcd.begin(16, 1);
Serial.begin(4800);
lcd.println("PAUL");
delay(2000);
lcd.clear();
lcd.println("Genereation-Set 10");
delay(2000);
lcd.clear();

//KWH interrupt attached to IRQ 1 = pin3 IRQ = Pin 21
attachInterrupt(2, onPulse, FALLING);
}

void loop()
{
}
void onPulse()
{
lastTime = pulseTime;
pulseTime = micros();

pulseCount++;
power = (3600000000.0 / (pulseTime - lastTime))/ppwh;
elapsedkWh = (1.0pulseCount/(ppwh1000));
lcd.clear();
//delay(1000);
lcd.println(power,0);
lcd.print(" KW");
lcd.println();
Serial.println(power,0);
Serial.print(" KW ");
//Serial.println();
}

OK. Now write a separate ISR for the second interrupt and attach that to the second interrupt pin.

Don't use Serial.print() in the ISRs, use different variable names in each ISR and declare any variables that are updated by the ISRs as volatile.

I suggest you simplify the ISR code to the following. And put the other code in loop() - anywhere but in the ISR

void onPulse()
{
pulseTime = micros();
pulseCount++;
newPulse = true;
}

Your code in loop() can check whether newPulse is true in order to know that a pulse has been detected.

...R

Your code in loop() can check whether newPulse is true in order to know that a pulse has been detected.

Be sure to set it back to false, somewhere.

thanks I'll give it a go!

PaulS:
Be sure to set it back to false, somewhere.

I left that as an exercise for the student :slight_smile:

...R