ArduinoTroubleshooting

MCP23017 withrotary encoder on a Arduino Leonardo

userHead Sean.Haynes 2024-10-14 04:45:51 19 Views0 Replies

Good afternoon all

I purchased a MCP23017  expansion card for my Arduino Leonardo board. the board is connected to the Leonardo using the supplied ribbon connected to the SDA / Pin 2 , SCL /Pin 3, Gnd and 5v.

 

I have a PEC11 rotary encoder which has 5 pins, 2 for the intergrated switch which works fine and 3 for the endcoder, 2 signal and one Gnd. I tried, very hard to understand the sketch here, but clearly I have done something wrong…..

https://github.com/DFRobot/DFRobot_MCP23017/blob/master/examples/ioInterrupt/ioInterrupt.ino

 

The objective is to be able to assign a rotary rotation to  specific buttons in a joystick library. Witin the code is a print function so it should print out a ‘CCW’ or ‘CW’ statement in the serial monitor interface as the rotary encoder is rotated. It will only print the ‘CCW’ statement - constantly, not just when turned? I have of course checked the wiring, but after playing with this for some time and having limited knowledge I was hoping someone could point me in the right direction. Many thanks.

 

#include <Joystick.h>

#include <Rotary.h>

#include <elapsedMillis.h>

#include <DFRobot_MCP23017.h>

DFRobot_MCP23017 mcp(Wire, 0x20);

bool intFlagA = false;  //INTA interrupt sign

bool intFlagB = false;  //INTB interrupt sign

#define Collective A0

int xAxisRotation_ = 90;

#define Throttle A1

int Throttle_ = 0;

#define Button1 1

#define Button2 4

#define Button3 5

#define Button4 6

#define Button5 7

#define Button6 8


 

#define Button7 mcp.eGPB0  //Encoder1 Button

//#define Button8 mcp.eGPB1  //Encoder2 Button


 

#define Button9 mcp.eGPA0   // Encoder CW

#define Button10 mcp.eGPB7  // Encoder CCW


 

elapsedMillis timeElapsed;

unsigned int t_interval = 200;   //interval to hold buttons on for when needed

unsigned int enc_interval = 50;  //interval for encoders

Rotary ROTARY1 = Rotary(mcp.eGPA0, mcp.eGPB7);


 

int lastButton1State = 0;

int lastButton2State = 0;

int lastButton3State = 0;

int lastButton4State = 0;

int lastButton5State = 0;

int lastButton6State = 0;

int lastButton7State = 0;

int lastButton8State = 0;

int lastButton9State = 0;

int lastButton10State = 0;

int counter = 0;


 

Joystick_ Joystick(0x04, JOYSTICK_TYPE_JOYSTICK,


 

                   12, 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();

  ROTARY1.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);

  pinMode(SDA, INPUT);  //Leonardo external interrupt 2

  pinMode(SCL, INPUT);  //Leonardo external interrupt 3

  Serial.begin(9600);


 

  mcp.pinModeInterrupt(mcp.eGPA0, mcp.eHighLevel, gpa0CB);

  mcp.pinModeInterrupt(mcp.eGPB7, mcp.eChangeLevel, gpb7CB);

  attachInterrupt(/*Interrupt NO*/ 0, notifyA, RISING);  //Enable external interrupt 0, connect INTA to the main-controller's digital pin: UNO(2),Mega2560(2),Leonardo(3),microbit(P0)

  attachInterrupt(/*Interrupt NO*/ 1, notifyB, RISING);  //Enable external interrupt 1, connect INTB to the main-controller's digital pin: UNO(3),Mega2560(3),Leonardo(2),microbit(P1)

}

void gpa0CB(int index) {

  String description = mcp.pinDescription(index);

  Serial.print(description);

  Serial.println(" Interruption CW!");

}

void gpb7CB(int index) {

  String description = mcp.pinDescription(index);

  Serial.print(description);

  Serial.println(" Interruption CCW!");

}

/*Interrupt service function*/

void notifyA() {

  intFlagA = true;

}

void notifyB() {

  intFlagB = true;

}

 

void loop() {

  if (intFlagA) {

    intFlagA = false;

    /*pollInterrupts function is used to poll if an interrupt occurs on a port group

    parameter group, the available parameter is shown below: (default value: eGPIOALL):

     eGPIOA        eGPIOB         eGPIOALL

     Port groupA  Port groupB   Port groupA+B

    */

    mcp.pollInterrupts(/*group = */ mcp.eGPIOA);

  }

  if (intFlagB) {

    intFlagB = false;

    mcp.pollInterrupts(/*group = */ mcp.eGPIOB);

  }



 

  Throttle_ = analogRead(A0) / 04;

  Throttle_ = map(Throttle_, 0, 255, 255, 0);

  Joystick.setThrottle(Throttle_);


 

  xAxisRotation_ = analogRead(A1);

  xAxisRotation_ = map(xAxisRotation_, 0, 1023, 0, 255);

  Joystick.setRxAxis(xAxisRotation_);


 

  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;

  }

  if (timeElapsed > enc_interval) {

    Joystick.setButton(9, 0);

    Joystick.setButton(10, 0);

  }

  Joystick.sendState();

}

void rotate() {


 

  // HANDLE ENCODER 1

  unsigned char result1 = ROTARY1.process();

  if (result1 == DIR_CW) {

    timeElapsed = 0;

    Joystick.setButton(9, 1);

    Joystick.setButton(10, 0);

  } else if (result1 == DIR_CCW) {

    timeElapsed = 0;

    Joystick.setButton(10, 1);

    Joystick.setButton(9, 0);





 

    //delay(100);

  }

}