Help needed with Arduino Mega 2560 Interrupts

Hi All,

I'm having an issue with using interrupts on a Meag2560. I have tested this on 2 other Megas and they are also showing the behavior, so I'm guessing there's something wrong in my code.

This is the code:

const byte FRCPin1 = 2;
const byte FRCPin2 = 19;
volatile byte PulseCount1, PulseCount2 = 0;
unsigned long FRC1StartMillis, FRC1StopMillis, FRC2StartMillis, FRC2StopMillis;
float FlowRateConstant = 7.5; //lt/m Calculated with current pump in use
float result = 0.0;

void setup() {
pinMode(FRCPin1, INPUT_PULLUP);
pinMode(FRCPin2, INPUT_PULLUP);
Serial.begin(115200);
}

void loop() {
PulseCount1 = 0;
PulseCount2 = 0;
attachInterrupt(digitalPinToInterrupt(FRCPin1), Flow1, RISING);
attachInterrupt(digitalPinToInterrupt(FRCPin2), Flow2, RISING);

delay(1000);

detachInterrupt(digitalPinToInterrupt(FRCPin1));
detachInterrupt(digitalPinToInterrupt(FRCPin2));
result = (PulseCount160) / FlowRateConstant; //(float)ElapsedMillis/(float)PulseCount1;
Serial.println("Flow1: "+String(PulseCount1)+"\t"+String(result,0)+"L/h");
result = (PulseCount2
60) / FlowRateConstant; //(float)ElapsedMillis/(float)PulseCount1;
Serial.println("Flow2: "+String(PulseCount2)+"\t"+String(result,0)+"L/h");
}

void Flow1()
{
PulseCount1++;
}

void Flow2()
{
PulseCount2++;
}

Read “How To Use This Forum”

in particular, 7. If you are posting code or error messages, use “code” tags

This is what happens when you do not

You didn't say what you are seeing vs. what you expected to see... Please describe this.

Also, your PulseCount variables are of type byte so they can only go up to 255 before rolling over to 0. How many pulses do you expect to receive in the 1 second you are waiting?

Finally, learn how to use C strings, not the "String" class, it fragments memory and will lead to trouble.

Please disregard the original message, I'm new to this and I hit the wrong button.

Hi All,

I'm having an issue with using interrupts on a Meag2560. I have tested this on 2 other Megas and they are also showing the behavior, so I'm guessing there's something wrong in my code.

This is the code:

const byte FRCPin1 = 18;
const byte FRCPin2 = 19;
volatile byte PulseCount1, PulseCount2 = 0;
unsigned long FRC1StartMillis, FRC1StopMillis, FRC2StartMillis, FRC2StopMillis;
float FlowRateConstant = 7.5; //lt/m Calculated with current pump in use
float result = 0.0;

void setup() {
  pinMode(FRCPin1, INPUT_PULLUP);
  pinMode(FRCPin2, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {
    PulseCount1 = 0;
    PulseCount2 = 0;
    attachInterrupt(digitalPinToInterrupt(FRCPin1), Flow1, RISING);
    attachInterrupt(digitalPinToInterrupt(FRCPin2), Flow2, RISING);
   
    delay(1000);

    detachInterrupt(digitalPinToInterrupt(FRCPin1));
    detachInterrupt(digitalPinToInterrupt(FRCPin2));
    result = (PulseCount1*60) / FlowRateConstant; //(float)ElapsedMillis/(float)PulseCount1;
    Serial.println("Flow1: "+String(PulseCount1)+"\t"+String(result,0)+"L/h");
    result = (PulseCount2*60) / FlowRateConstant; //(float)ElapsedMillis/(float)PulseCount1;
    Serial.println("Flow2: "+String(PulseCount2)+"\t"+String(result,0)+"L/h");
}

void Flow1()
{
  PulseCount1++;
}

void Flow2()
{
  PulseCount2++;
}

I am connecting a function generator to Int4 & Int5.
Int4 - square wave @100Hz
Int5 - square wave @ 90Hz

The output I'm getting is not 100 pulses and 90 pulses respectively but it's around 150 pulses and 120 Pulses respectively.

If I disconnect the generator from Int5 I get 100 pulses output on both. Likewise if I disconnect Int4 and reconnect Int5 I get 90 pulses on both. It's almost like there's a short between Int4 & Int5. I have checked for this and there isn't.

I don't understand what is going on...

If I use Int0 and Int4 I don't get the same output on both counts if I disconnect one signal, but I still get incorrect output counts when both signals are connected.

Am I doing something wrong in my code?

Thanks and my apologies for the first post.

Robbie...

What voltage is your signal generator outputting?
Is everything sharing a common ground?

Signal generator outputting 5V and yes sharing common ground.

I'm using a FeelTech FY2300 signal generator. It is just a cheap one from eBay.

There's a couple major problems with this code:

  • You shouldn't be attaching / detaching the ISR on every pass through the loop() function.

  • The local variables PulseCount1 and PulseCount2 are shadowing the global variables of the same name.

Hi gfvalvo,

Thanks for the reply.

There's a couple major problems with this code:

  • You shouldn't be attaching / detaching the ISR on every pass through the loop() function.

Ok, should I be using interrupts() and noInterrupts() instead?

  • The local variables PulseCount1 and PulseCount2 are shadowing the global variables of the same name.

Those variables are declared as volatile byte as globals aren't they?
Can you please explain, as I'm unsure of what you mean.

Kind Regards,
Robbie...

gfvalvo:

  • The local variables PulseCount1 and PulseCount2 are shadowing the global variables of the same name.

The posted code does not have any local variables. loop() does reset them to 0, but no local declarations.

blh64:
The posted code does not have any local variables. loop() does reset them to 0, but no local declarations.

Right, I looked but didn't see correctly.

Hi All,

I have figured it out.

Stupid me, my function generator was set to AC signal.
Once I set it to DC, it all worked fine.

Sorry to bother you guys with my stupidity :blush:

Robbie...