Afternoon all - I have been playing with making a controller using various toggle and rotary switches - it became apparent that there were not going to be enough pins on the Arduino Leonardo board that I have. So I purchased an expansion board - SDFRobot MCP23017.
Not having used one before I loaded the example button sketch onto the Leonardo so I could figure out how it works. It seemd fairly straight forward - so I then started to incorporate the code into the existing joystick sketch I had been working on. Initially everything seemed to be working fine, but when I came back the next day to carry on I had all sorts of issues. I had thought that perhaps a wire had come lose, but having checked all the wires that isn;t the case.
If I load the sketch removing all instances that refer to the MCP board , the 3 toggle switches I have assigned on the Leonardo work perfectly - each toggle is on-off-on, or on-off-mom - so 6 pins in all assigned on the Leonardo. 1-6
I have a seperate on-off-on toggle as an experiement on the MCP board. If I load the sketch below but DO NOT attach any of the 3 toggle switches to the Leonardo, the switch on the MCP works fine.
If however I attach a toggle switch to pins1 and 2 on the Leaornado, after a few seconds of operation the board locks up so I have to reboot the card. The same thing happens if I use the exact same switch on pins 3 and 4........however if I attach the exact same switch to pins 5 and 6 it works perfectly in unison with the toggle switch attached to the MCP card!
So in short for some reason I can't use pins 1-4 on the Leonardo with the MCP configured.
I am at a loss - it seems like there is some sort of addressing issue going on but I don't have that indepth kind of knowledge - hoping someone here does?
#include <Joystick.h>
#include <DFRobot_MCP23017.h>
DFRobot_MCP23017 mcp(Wire, 0x20);
#define Collective A0
int rxAxis_ = 0;
#define Throttle A1
int Throttle_ = 0;
#define Button1 PD1
#define Button2 PD2
#define Button3 PD3
#define Button4 PD4
#define Button5 PD5
#define Button6 PD6
#define Button7 mcp.eGPB0
#define Button8 mcp.eGPB1
int lastButton1State = 0;
int lastButton2State = 0;
int lastButton3State = 0;
int lastButton4State = 0;
int lastButton5State = 0;
int lastButton6State = 0;
int lastButton7State = 0;
int lastButton8State = 0;
Joystick_ Joystick(0x08, JOYSTICK_TYPE_JOYSTICK,
9, 0, // Button Count, Hat Switch Count
false, false, false, // X and Y, Z Axis
true, false, false, // Rx, Ry, or Rz
false, true, // rudder, throttle
false, false, false); // accelerator, brake, or steering
const bool initAutoSendState = true;
void setup() {
Joystick.begin();
mcp.begin();
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
mcp.pinMode(mcp.eGPB0, INPUT_PULLUP);
mcp.pinMode(mcp.eGPB1, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
Throttle_ = analogRead(Throttle) / 04;
Throttle_ = map(Throttle_, 0, 1023, 0, 255);
Joystick.setThrottle(Throttle_);
rxAxis_ = analogRead(Collective);
rxAxis_ = map(rxAxis_, 0, 1023, 0, 255);
Joystick.setRxAxis(rxAxis_);
int currentButton1State = !digitalRead(Button1);
if (currentButton1State != lastButton1State) {
Joystick.setButton(0, currentButton1State);
lastButton1State = currentButton1State;
}
int currentButton2State = !digitalRead(Button2);
if (currentButton2State != lastButton2State) {
Joystick.setButton(1, currentButton2State);
lastButton2State = currentButton2State;
}
int currentButton3State = !digitalRead(Button3);
if (currentButton3State != lastButton3State) {
Joystick.setButton(2, currentButton3State);
lastButton3State = currentButton3State;
}
int currentButton4State = !digitalRead(Button4);
if (currentButton4State != lastButton4State) {
Joystick.setButton(3, currentButton4State);
lastButton4State = currentButton4State;
}
int currentButton5State = !digitalRead(Button5);
if (currentButton5State != lastButton5State) {
Joystick.setButton(4, currentButton5State);
lastButton5State = currentButton5State;
}
int currentButton6State = !digitalRead(Button6);
if (currentButton6State != lastButton6State) {
Joystick.setButton(5, currentButton6State);
lastButton6State = currentButton6State;
}
int currentButton7State = !mcp.digitalRead(Button7);
if (currentButton7State != lastButton7State) {
Joystick.setButton(6, currentButton7State);
lastButton7State = currentButton7State;
}
int currentButton8State = !mcp.digitalRead(Button8);
if (currentButton8State != lastButton8State) {
Joystick.setButton(7, currentButton8State);
lastButton8State = currentButton8State;
}
Joystick.sendState();
delay(100);
}