Lack of digital interrupt pins ?

Hello everyone !

I'm anticipating my thoughts about a little project I'm doing right now, not sure if this will be really a problem in the future, hence I'm taking precautions.

I'll need to read 3 Water Flow sensors. And a MEGA 2560 board is being used. I'm using the following function to measure the volume flow :

double volumeFlow(byte sensorInterrupt,byte sensorPin, unsigned long desiredVolume){


float fatorCalibrador = 4.5;
volatile byte contaPulso = 0; 
float taxaFluxo = 0.0;
unsigned int fluxoL = 0;
unsigned long totalL = 0;
unsigned long oldTime = 0;

pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
attachInterrupt(sensorInterrupt, contadorPulso, FALLING);

    if(desiredVolume> totalL){ 
        if((millis() - oldTime) > 1000){                     
            detachInterrupt(sensorInterrupt);
            taxaFluxo = ((1000.0 / (millis() - oldTime)) * contaPulso) / fatorCalibrador;
            oldTime = millis();
            fluxoL = (taxaFluxo / 60);
            totalL += fluxoL; 
            unsigned int fracao;
            fracao = (taxaFluxo - int(taxaFluxo)) * 10;
            contaPulso = 0;
            attachInterrupt(sensorInterrupt, contadorPulso, FALLING);
        }
    }
    return totalL;

}

As you can see, sensorInterrupt is also passed as parameter to attachInterrupt(). Reading more about this function (attachInterrupt) I saw a table which got me worried :frowning:

pins 2, 3, 18, 19, 20, 21 are the ones which are "interrupt functional" in my board. I have pin 2 and 3 available. 18,19,20,21 are taken by a RTC shield.

DEFINING :

#define PIN_INTERRUPT_T 0 // int 0 = pin 2 I guess
#define PIN_INTERRUPT_S 1 // int 1 = pin 3 I guess
#define PINO_INTERRUPT_I 2 // No idea what int 2 is equivalent, my trouble is here.
#define PIN_SENSOR_T 6
#define PIN_SENSOR_S 5
#define PIN_SENSOR_I 8

Would I be able to call this function to the 3 Water Flow sensor? For example :

volumeFlow(0,6,desiredVolume) - This to Water Flow T
volumeFlow(1,5,desiredVolume) - This for Water Flow S
volumeFlow(2,8,desiredVolume) - This for the Water Flow I

Is there any idea to counter this possible problem? I'm afraid I did not understood the pin map right.

This should help

Really much thanks

That is one of the disadvantages of using a shield.

However if you use pin change interrupts you can have an interrupt working on almost any I/O pin.

...R

Robin2:
That is one of the disadvantages of using a shield.

However if you use pin change interrupts you can have an interrupt working on almost any I/O pin.

...R

Do you mean digitalToPin() ?

Do you mean digitalToPin() ?

No. There are "pin change interrupts" and "external interrupts" available to use to detect a change of state on a pin. It is only the external interrupts which are available with the attachInterrupt() function of the ide.

I recommend that you use Nico Hood's library for pin change interrupts. It is available through the library manager as well as github GitHub - NicoHood/PinChangeInterrupt: A simple & compact PinChangeInterrupt library for Arduino

Perhaps, interrupts are not needed at all.

Not all pins on the '2560 support pin change interrupts. Check that out as well.

cattledog:
No. There are "pin change interrupts" and "external interrupts" available to use to detect a change of state on a pin. It is only the external interrupts which are available with the attachInterrupt() function of the ide.

I recommend that you use Nico Hood's library for pin change interrupts. It is available through the library manager as well as github GitHub - NicoHood/PinChangeInterrupt: A simple & compact PinChangeInterrupt library for Arduino

Thanks to your reply. Things are a bit cloudy for me still. So this library can "convert" standart Not-Usable Digital Pins For Interrupts to "Usable pins for interrupts" ?

If so, I should change the volumeFlow function. All the lines containing attachInterrupt should be replaced to attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(pin), contadorPulso, FALLING);

Thank you so much !

rezik:
Things are a bit cloudy for me still. So this library can "convert" standart Not-Usable Digital Pins For Interrupts to "Usable pins for interrupts" ?

That statement is itself cloudy :slight_smile:

There is no question of "converting" anything. The pinChange interrupt capability always exists - it is just a case of making use of it. And the pinChange interrupts work somewhat differently from the external interrupts.

You should read the Atmega 2560 datasheet to get the full details.

...R

Things are a bit cloudy for me still. So this library can "convert" standart Not-Usable Digital Pins For Interrupts to "Usable pins for interrupts" ?

No. This library lets you use those pins which support pin change interrupts with simple syntax analagous to the syntax for external interrupts. Otherwise, you would be using low level register settings to enable the pin change interrupt, and additional coding to work out which pin within a group of pins on the same interrupt changed, and how it changed.

All the lines containing attachInterrupt should be replaced to attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(pin), contadorPulso, FALLING);

That looks correct. The"pin" must be one which supports pin change interrupts

Arduino Mega: 10, 11, 12, 13, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64),
A11 (65), A12 (66), A13 (67), A14 (68), A15 (69)

I assume you will need a separate contadorPulso for each flowmeter.

You should not be attaching and detaching the interrupts in your code. Take a look at Nick Gammons tutorial on interrupts to see how to briefly disable interrups, safely transfer data, and then reenable them. Gammon Forum : Electronics : Microprocessors : Interrupts

Hi,
I have the following dumb suggestion. What about "ored" all the PIN input to an interrupt and when the interrupt it is trigger the program read all the digital inputs find out which trigger the interrupt and do whatever need to do for that digital input. I think it make work. The only problem it is that all the digital inputs most be at zero. So any 1 will trigger the interrupt. You can use diodes to make the OR

tauro0221:
What about "ored" all the PIN input to an interrupt

Isn't it easier to use the in-built pinChange interrupt feature?

...R