Joystick.h and DFRobot_MCP23017.h expansion board

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);
}

you forgot to share a schematic.

#include <Joystick.h>
#include <DFRobot_MCP23017.h>
DFRobot_MCP23017 mcp(Wire, 0x20);
#define Collective A0
#define Throttle A1

Joystick_ Joystick(0x08, JOYSTICK_TYPE_JOYSTICK,
                   32, 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

void setup() {
  Joystick.begin();
  mcp.begin();
  for (int i = 0; i < 16; i++)mcp.pinMode(i, INPUT_PULLUP); // 16 buttons or poles of switches are connected to 16 input pins of MCP23017 module
  for (int i = 4; i < 14; i++)pinMode(i, INPUT_PULLUP); // and 10 are connected to pins 4-13 of Leonardo; pins 2 and 3 are used as I2C line for MCP23017
}


void loop() {
  Joystick.setThrottle(analogRead(Throttle));
  Joystick.setRxAxis(analogRead(Collective));

  for (int i = 0; i < 16; i++)Joystick.setButton(i, mcp.digitalRead(i));
  for (int i = 4; i < 14; i++)Joystick.setButton(i + 16, digitalRead(i));

  delay(50);
}

RESOLVED - I found out what was happening. Though the Leonardo has what seems to be dedicated SCL and SDA connectors on the top right of the card, they are in fact only extensions of pins d2 and d3. As I had configured toggle switches to use those pins it was causing the issue. I tested by attaching a toggle switch to pins D1 and D4, all work as expected.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.