Here is my serial output (for when I press and hold the button on pin 8):
MCP23xxx Interrupt Test!
Interrupt detected on pin: 8
Pin states at time of interrupt: 0b111000000000
Pin 8 was pressed
Interrupt detected on pin: 8
Pin states at time of interrupt: 0b111000000000
Pin 8 was pressed
Interrupt detected on pin: 8
Pin states at time of interrupt: 0b111000000000
Pin 8 was pressed
Interrupt detected on pin: 8
Pin states at time of interrupt: 0b111000000000
Pin 8 was pressed
Interrupt detected on pin: 8
Pin states at time of interrupt: 0b111000000000
Pin 8 was pressed
Here is my code:
Code: Select all
// NOTE: This is a simple example that only reads the INTA or INTB pin
// state. No actual interrupts are used on the host microcontroller.
// MCP23XXX supports the following interrupt modes:
// * CHANGE - interrupt occurs if pin changes to opposite state
// * LOW - interrupt occurs while pin state is LOW
// * HIGH - interrupt occurs while pin state is HIGH
#include <Arduino.h>
#include <Adafruit_MCP23X17.h>
#define CHECK_PIN_PRESSED(var,pos) (!((var) & (1<<(pos))))
#define BUTTON_PIN 1 // MCP23XXX pin used for interrupt
#define INT_PIN 4 // microcontroller pin attached to INTA/B
int clicks = 0;
Adafruit_MCP23X17 mcp;
void setup() {
Serial.begin(115200);
//while (!Serial);
Serial.println("MCP23xxx Interrupt Test!");
// uncomment appropriate mcp.begin
if (!mcp.begin_I2C())
{
Serial.println("Error.");
while (1);
}
// configure MCU pin that will read INTA/B state
pinMode(INT_PIN, INPUT);
mcp.setupInterrupts(false, false, LOW);
// configure button pins for input with pull up
mcp.pinMode(8, INPUT_PULLUP);
mcp.pinMode(9, INPUT_PULLUP);
mcp.pinMode(10, INPUT_PULLUP);
mcp.pinMode(11, INPUT_PULLUP);
// enable interrupts on button press
mcp.setupInterruptPin(8, LOW);
mcp.setupInterruptPin(9, LOW);
mcp.setupInterruptPin(10, LOW);
mcp.setupInterruptPin(11, LOW);
}
void loop()
{
if (!digitalRead(INT_PIN))
{
uint16_t allPins = mcp.getCapturedInterrupt();
uint8_t lastPin = mcp.getLastInterruptPin();
Serial.print("Interrupt detected on pin: ");
Serial.println(lastPin);
Serial.print("Pin states at time of interrupt: 0b");
Serial.println(allPins, 2);
if (CHECK_PIN_PRESSED(allPins, 8))
{
Serial.println ("Pin 8 was pressed");
}
if (!(allPins & (1<<(9))))
{
Serial.println ("Pin 9 was pressed");
}
if (!(allPins & (1<<(10))))
{
Serial.println ("Pin 10 was pressed");
}
if (!(allPins & (1<<(11))))
{
Serial.println ("Pin 11 was pressed");
}
delay(250); // debounce
// NOTE: If using DEFVAL, INT clears only if interrupt
// condition does not exist.
// See Fig 1-7 in datasheet.
mcp.clearInterrupts(); // clear
}
}