I'm posting some test code that I'm having trouble getting to compile. It's intended for an Arduino Uno, but I'm just using the "Verify" button in the IDE right now.
JC_Button library: GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses
adafruit MCP23017 library: GitHub - adafruit/Adafruit-MCP23017-Arduino-Library: Arduino Library for Adafruit MCP23017
According to the JC_Button library examples, you create your button objects like so:
#include <JC_Button.h>
Button MyButton(pinID#); // defines the button and assigns it to pin pinID#
void setup()
{
MyButton.begin();
}
The MCP23017 I/O extender is setup and used thusly:
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp;
void setup()
{
mcp.begin();
mcp.pinMode(0, INPUT);
}
I'm having trouble trying to use the JC_Button library to create button objects attached to pins on the I/O extender. Here are some of my attempts, and the results when I hit the "Verify" button:
Attempt #1:
#include <JC_Button.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
mcp.Button MyBtn01(0);
mcp.Button MyBtn02(1);
void setup()
{
mcp.begin();
MyBtn01.begin();
MyBtn02.begin();
}
void loop()
{
}
This resulted in the error:
TestCode:7:1: error: 'mcp' does not name a type
mcp.Button MyBtn01(0);
^~~
TestCode:8:1: error: 'mcp' does not name a type
mcp.Button MyBtn02(1);
^~~
C:\Users\dloyer\Documents\Arduino\TestCode\TestCode.ino: In function 'void setup()':
TestCode:13:3: error: 'MyBtn01' was not declared in this scope
MyBtn01.begin();
^~~~~~~
TestCode:14:3: error: 'MyBtn02' was not declared in this scope
MyBtn02.begin();
^~~~~~~
exit status 1
'mcp' does not name a type
Thinking the issue is that the I/O expander needed to be instantiated before I attempted to use it to define my button object, I pulled it out of setup and placed it before the creation of my button objects.
Attempt #2:
#include <JC_Button.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
mcp.begin();
mcp.Button MyBtn01(0);
mcp.Button MyBtn02(1);
void setup()
{
MyBtn01.begin();
MyBtn02.begin();
}
void loop()
{
}
Which resulted in this error:
TestCode:7:1: error: 'mcp' does not name a type
mcp.begin();
^~~
TestCode:9:1: error: 'mcp' does not name a type
mcp.Button MyBtn01(0);
^~~
TestCode:10:1: error: 'mcp' does not name a type
mcp.Button MyBtn02(1);
^~~
C:\Users\dloyer\Documents\Arduino\TestCode\TestCode.ino: In function 'void setup()':
TestCode:14:3: error: 'MyBtn01' was not declared in this scope
MyBtn01.begin();
^~~~~~~
TestCode:15:3: error: 'MyBtn02' was not declared in this scope
MyBtn02.begin();
^~~~~~~
exit status 1
'mcp' does not name a type
OK, maybe I needed to move the button creation into Setup instead
Attempt #3:
#include <JC_Button.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
void setup()
{
mcp.begin();
mcp.Button MyBtn01(0);
mcp.Button MyBtn02(1);
MyBtn01.begin();
MyBtn02.begin();
}
void loop()
{
}
Still no luck, but a defferent error:
C:\Users\dloyer\Documents\Arduino\TestCode\TestCode.ino: In function 'void setup()':
TestCode:13:7: error: 'class Adafruit_MCP23017' has no member named 'Button'
mcp.Button MyBtn01(0);
^~~~~~
TestCode:14:7: error: 'class Adafruit_MCP23017' has no member named 'Button'
mcp.Button MyBtn02(1);
^~~~~~
TestCode:15:3: error: 'MyBtn01' was not declared in this scope
MyBtn01.begin();
^~~~~~~
TestCode:16:3: error: 'MyBtn02' was not declared in this scope
MyBtn02.begin();
^~~~~~~
exit status 1
'class Adafruit_MCP23017' has no member named 'Button'
Out of desperation, I moved the mcp instatiation out of setup again, and tried moving the reference to the mcp pin ID into the button call instead.
Attempt #4:
#include <JC_Button.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
mcp.begin();
Button MyBtn01(mcp(0));
Button MyBtn02(mcp(1));
void setup()
{
MyBtn01.begin();
MyBtn02.begin();
}
void loop()
{
}
With this result:
TestCode:7:1: error: 'mcp' does not name a type
mcp.begin();
^~~
TestCode:8:21: error: no match for call to '(Adafruit_MCP23017) (int)'
Button MyBtn01(mcp(0));
^
TestCode:9:21: error: no match for call to '(Adafruit_MCP23017) (int)'
Button MyBtn02(mcp(1));
^
exit status 1
'mcp' does not name a type
For my last trick, I tried moving it all back into setup again.
Attempt #5:
#include <JC_Button.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
void setup()
{
mcp.begin();
Button MyBtn01(mcp(0));
Button MyBtn02(mcp(1));
MyBtn01.begin();
MyBtn02.begin();
}
void loop()
{
}
And got this result:
C:\Users\dloyer\Documents\Arduino\TestCode\TestCode.ino: In function 'void setup()':
TestCode:13:23: error: no match for call to '(Adafruit_MCP23017) (int)'
Button MyBtn01(mcp(0));
^
TestCode:14:23: error: no match for call to '(Adafruit_MCP23017) (int)'
Button MyBtn02(mcp(1));
^
exit status 1
no match for call to '(Adafruit_MCP23017) (int)'
I'm now grasping at straws. Are these two libraries simply incompatable with eachother?
Is there maybe a way I can define something as a pin on the I/O extender and then use that in the button creation?
I.E.
#define BlahBlah mcpPin#
Button MyButton(BlahBlah);
void setup()
{
MyButton.begin();
}
Or is there some way of easily modifying either library so they play nice together? Or is there a different library for the MCP23017 that will work with the JC_Button library?
Thanks for any insight you can give me,
Dennis