I am trying to use the interrupts on the Adafruit MCP23017 GPIO Expander and I just can't seem to figure out what I am doing wrong. Currently I have pin 7 of my Metro Mini connected the INTA port, that based on what I am reading in Adafruit's documentation controls the A0 - A7 ports of the MCP2301. I have a button connected to port A1 on the MCP23017. When I try to run my program the button doesn't trigger anything. I have a section of code that uses serial print to read the current state of the button, but the state of the button is always high.
Unfortunately, I haven't been able to find a decent tutorial on using the Adafruit MCP23017 library that uses the INTA ports. So I tried to cobble some code together base on one of the examples that is given in the Adafruit Library.
Here is my code:
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_MCP23X17.h>
Adafruit_MCP23X17 mcp;
boolean flag1 = false;
boolean flag2 = false;
const int LED_PIN = 12;
const int neoBtnPins[2] = {2, 3};
const int usrNodeArray[4] = {0,1,2,3};
const long debounceTime = 50;
const long interval = 5000;
unsigned long prevMillis = 0;
static uint32_t lastPressTime = 0;
uint32_t neoPixArray[8];
uint32_t rmdPixArray[4];
uint32_t usrPixArray[4];
int colorIDX = 0;
int selectIDX = 0;
int usrNodeIDX = 0;
int usrNodeInc = 0;
volatile byte state = LOW;
void cycleColor();
void selectColor();
void asnColor();
void chkColorCode();
void randomize();
uint32_t Red;
uint32_t Blue;
uint32_t Yellow;
uint32_t Green;
uint32_t Gray;
uint32_t Magenta;
uint32_t Navy;
uint32_t Brown;
#define LED_COUNT 60
#define irqPinMCU 7 //Pin 7 on Metro Mini
#define btnSubmit 1 // A1 on Adafruit MCP23017 I/O Expander
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
Serial.begin(9600);
mcp.begin_I2C();
strip.begin();
strip.show();
strip.setBrightness(10);
pinMode(irqPinMCU, INPUT);
mcp.setupInterrupts(true, false, LOW);
mcp.pinMode(btnSubmit, INPUT_PULLUP);
mcp.setupInterruptPin(btnSubmit, HIGH);
for(int i = 0; i < 2 ; i++ )
{
pinMode(neoBtnPins[i], INPUT);
}
attachInterrupt(digitalPinToInterrupt(neoBtnPins[0]), cycleColor, CHANGE);
attachInterrupt(digitalPinToInterrupt(neoBtnPins[1]), selectColor, CHANGE);
Red = strip.Color(139, 0, 0);
Green = strip.Color(0, 100, 0);
Yellow = strip.Color(139, 139, 0);
Blue = strip.Color(0, 0, 139);
Gray = strip.Color(192, 192, 192);
Magenta = strip.Color(255, 69, 0);
Navy = strip.Color(25, 25, 112);
Brown = strip.Color(139, 69, 19);
neoPixArray[0] = Red;
neoPixArray[1] = Green;
neoPixArray[2] = Yellow;
neoPixArray[3] = Blue;
neoPixArray[4] = Gray;
neoPixArray[5] = Magenta;
neoPixArray[6] = Navy;
neoPixArray[7] = Brown;
for( int n = 0; n < 4; n++ )
{
randomize();
uint32_t randomColor = random(0,7);
rmdPixArray[n] = neoPixArray[randomColor];
}
}
void loop()
{
strip.setPixelColor(4,rmdPixArray[0]);
strip.setPixelColor(5,rmdPixArray[1]);
strip.setPixelColor(6,rmdPixArray[2]);
strip.setPixelColor(7,rmdPixArray[3]);
strip.show();
if(usrNodeInc <= 3)
{
if(flag2 == false)
{
Serial.print("usrNodeInc is ");
Serial.print("\t");
Serial.print(usrNodeInc);
Serial.println();
flag2 = true;
}
if(colorIDX <= 7 /*&& usrNodeInc < 3*/)
{
strip.setPixelColor(usrNodeArray[usrNodeInc], neoPixArray[colorIDX]);
strip.show();
}
else
{
colorIDX = 0;
}
}
else
{
//chkColorCode();
}
/*if(!digitalRead(irqPinMCU) && millis() - lastPressTime > debounceTime)
{
Serial.println("Submit Button is Working");
mcp.clearInterrupts();
}*/
Serial.print("irqPinMCu Status ");
Serial.print("\t");
Serial.print(digitalRead(irqPinMCU));
Serial.println();
mcp.clearInterrupts();
}
void cycleColor()
{
if(digitalRead(neoBtnPins[0]) && millis() - lastPressTime > debounceTime)
{
lastPressTime = millis();
state = !state;
colorIDX++;
flag1 = false;
}
}
void selectColor()
{
if(digitalRead(neoBtnPins[1]) && millis() - lastPressTime > debounceTime)
{
lastPressTime = millis();
state = !state;
asnColor();
usrNodeInc++;
colorIDX = 0;
//flag2 = false;
//flag1 = false;
//chkColorCode();
}
}
void asnColor()
{
usrPixArray[usrNodeIDX] = neoPixArray[colorIDX];
if(flag1 == false)
{
Serial.print("usrPixArray");
Serial.print("\t");
Serial.print(usrPixArray[usrNodeIDX]);
Serial.println();
flag1 = true;
}
}
void randomize()
{
uint32_t newSeed = 0;
for (int i=0; i < 32; i++)
{
uint32_t r = analogRead(A0);
r <<= (analogRead(A1) >> 3) & 0x03;
r += analogRead(A2);
newSeed <<= 1;
if (r & 0x04)
{
newSeed |= 0x1;
}
}
randomSeed(newSeed);
}
void chkColorCode()
{
//if(flag1 == false)
//{
for(int test01 = 0; test01 <=3; test01++)
{
Serial.print("usrPixArray 0 is");
Serial.print("\t");
Serial.print(usrPixArray[test01]);
Serial.println();
}
//flag1 = true;
// }
}