I'm working on a project where I am planning to use five potential interrupts (four float sensors and a manual switch for pausing). I was planning to use five of the available interrupt pins (Digital pin 2, Digital pin 3, Digital pin 21, Digital pin 20, Digital pin 19 and Digital pin 18). However, I was also planning to use an LCD display and a sensor that both use I²C that I was planning to connect with a I²C hub.
For peripherals in my project, I have four analogue pins and 20 digital pins used.
Pin 20 and 21 are the SDA/SCL (respectively), but they are also the same pins as the two up at the AREF on the Mega. Is there any way to use either pin 20 or 21 as an interrupt pin and connect the I²C to the two pins at the AREF?
Looking at other options, I might be better to go with an Arduino Due where all of the digital pins can be an interrupt. I also found the Arduino Giga, where all of the GPIO pins are capable of interrupts and that has internal memory (which I think means that I can do away with the microSD card reader that I was going to use and also be able to access data that I am recording on a .csv file through the USB rather than having to take things apart to access the card). Unfortunately, both of these board operate on 3.3 V, and two of my planned sensors run up to 4.5 V (and I am not sure yet how to address this if I switch to a one of these two boards).
They generally speaking do not need interrupts.
Using interrupts on slow changing sensors could introduce more problems.
Switches usually need de-bouncing, and that must be done in loop() anyway.
Leo..
There is no need for a hub, as I2C supports connecting multiple devices using just two wires shared across all devices.
On a 7-bit I2C address, there are a total of 128 possible addresses (from 0x00 to 0x7F in hexadecimal, or 0 to 127 in decimal). But, not all of these are usable as some are reserved for specific purposes.
Out of the 128 total addresses:
16 addresses are reserved (0x00 to 0x07 and 0x78 to 0x7F).
This leaves you 112 usable addresses.
Therefore, on a 7-bit I2C bus, you can have up to 112 devices with unique addresses.
Sorry, I should have been more clear with how I wrote that. They are often called float sensors, but they are essentially an on/off switch. I am planning to use a debounce loop on the switches to get clearer readings.
also consider the ESP32 plenty of GPIOs (All GPIOs can be configured as interrupts), ADCs, two DACs, onboard WiFi, Bluetooth Classic and BLE - lower cost than DUE
That's essentially what the hub does. I already have a mess of wires with my planned project, and it's looking like I might have two breadboards in use (or at least one bread board with four different voltages), so I decided to use a DFR0759 I²C hub to try to simplify things and keep the wires straight. However, if I switch to the Arduino Giga and the Giga display shield, I can remove the I²C display and then I won't need a hub.
I know. Had one on the bilge pump in my sailboat. I still see no reason for interrupts.
Interrupts are for things that need to be taken care off much faster than the loop time. Float switches and manual switches that could bounce much longer than the loop time don't fall in that category. Just use polling.
I think "I²C hub" is confusing people. It's just a breadboard with pins, no electronics.
Leo..
Then what would you recommend as an alternative? I figured that it's a lot simpler to have the interrupt running the background than it is to have a flag in the operating loop and taking up time when I am already running a bunch of other stuff in that loop that requires a bunch of math and taking up processing time when I am trying to keep a stepper motor running at the proper speed.
Debouncing a switch is pretty much required. I want to be able to push a button and have the system stop or have the float sensors simply switch off the system, hence the interrupts that turn off the pump so that I don't break my system. When the float sensors turn off or the switch gets flipped back, I am going to need the debounce.
Debouncing and interrupts are opposite things, which are quite difficult to combine.
An interrupts implies immediately reaction, while debouncing needs a delay...
There is no "background" with a Mega2560. It can only execute one thing at a time. When the interrupt service routine (ISR) is triggered, the main loop stops until the ISR is finished. Doing a debounce in an ISR would stop the main loop for the debounce period, that's like using delays in your main loop for debouncing.
If you need to do something right away when a float switch is triggered, you can do it in the ISR if it's just a few lines of code but you will need to set a flag for the main loop so it can take appropriate actions.