Arduino Leonardo Matrix input to Joystick output

I have a little experience with coding and have made this compound scrip that takes a matrix button input (From a keypad) and outputs it to my pc in the form of a joystick button.

#include <Joystick.h>
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 3;
char Keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'A','B','C'}
};
byte rowPins[ROWS] = {3, 8, 7, 5};
byte colPins[COLS] = {4, 2, 6};
char customKey = 0;

Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); 
Joystick_ Buttons(0x03,0x05,12,false,false,false,false,false,false,false,false,false,false,false);
void setup(){
  Serial.begin(9600);
  Buttons.begin();
}
  
void loop(){
  customKey = customKeypad.getKey();
  if (customKey){
    Serial.println(customKey);
    if(customKey == '1'){
      Buttons.pressButton(0);
      delay(100);
      Buttons.releaseButton(0);
    }
    if(customKey == '2'){
      Buttons.pressButton(1);
      delay(100);
      Buttons.releaseButton(1);
    }
    if(customKey == '3'){
      Buttons.pressButton(2);
      delay(100);
      Buttons.releaseButton(2);
    }
    if(customKey == '4'){
      Buttons.pressButton(3);
      delay(100);
      Buttons.releaseButton(3);
    }
    if(customKey == '5'){
      Buttons.pressButton(4);
      delay(100);
      Buttons.releaseButton(4);
    }
    if(customKey == '6'){
      Buttons.pressButton(5);
      delay(100);
      Buttons.releaseButton(5);
    }
    if(customKey == '7'){
      Buttons.pressButton(6);
      delay(100);
      Buttons.releaseButton(6);
    }
    if(customKey == '8'){
      Buttons.pressButton(7);
      delay(100);
      Buttons.releaseButton(7);
    }
    if(customKey == '9'){
      Buttons.pressButton(8);
      delay(100);
      Buttons.releaseButton(8);
    }
    if(customKey == 'A'){
      Buttons.pressButton(9);
      delay(100);
      Buttons.releaseButton(9);
    }
    if(customKey == 'B'){
      Buttons.pressButton(10);
      delay(100);
      Buttons.releaseButton(10);
    }
    if(customKey == 'C'){
      Buttons.pressButton(11);
      delay(100);
      Buttons.releaseButton(11);
    }
  }
}

However to get it to work every time the board is reset to get it to work i need to comment out all the if statements and the Buttons.begin(), upload that, then uncomment the Buttons.begin() reupload and then finally uncomment the rest reupload and it works. As you can probably tell this is a horrid process but i have no idea why its happening and how to fix it. Any help would be appreciated.

So i have made my own code for the matrix read (Ugly as anything but) Now it works perfectly. I think it must be some interaction between the libraries i am using. I do have a solution however its ugly so if anyone has any better solutions i would still appreciate it.

Joystick_ Buttons(0x03,0x05,12,false,false,false,false,false,false,false,false,false,false,false);

Out of interest, what are the parameters to this function ?

0x03 - HID number - default
0x05 - Sets as a numpad
12 - button count
all the false's are for axis

Try to add while(!Serial); after Serial.begin();. It might solve the problem.

Or get rid of all Serial related stuff.

Not sure if it will help but worth a try.

Mind sharing your new code?

Have tried removing all serial still same issue.

Its just changing the matrix read to a manual way.

#include <Joystick.h>

const byte ROWS = 4;
const byte COLS = 3;
char Keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'A','B','C'}
};
byte rowPins[ROWS] = {3, 8, 7, 5};
byte colPins[COLS] = {4, 2, 6};
char customKey = 0;

Joystick_ Buttons(0x03,0x05,12,false,false,false,false,false,false,false,false,false,false,false);
void setup(){
  Buttons.begin();
  pinMode(4,INPUT_PULLUP);
  pinMode(2,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
  pinMode(3,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(7,OUTPUT);
}



void loop(){
  digitalWrite(3,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(5,LOW);
  if(digitalRead(4) == LOW){
    Buttons.pressButton(9);
    delay(100);
    Buttons.releaseButton(9);   
  }
  if(digitalRead(2) == LOW){
    Buttons.pressButton(10);
    delay(100);
    Buttons.releaseButton(10);   
  }
  if(digitalRead(6) == LOW){
    Buttons.pressButton(11);
    delay(100);
    Buttons.releaseButton(11);   
  }    
  digitalWrite(3,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(7,LOW);
  digitalWrite(5,HIGH);
  if(digitalRead(4) == LOW){
    Buttons.pressButton(6);
    delay(100);
    Buttons.releaseButton(6);   
  }
  if(digitalRead(2) == LOW){
    Buttons.pressButton(7);
    delay(100);
    Buttons.releaseButton(7);   
  }
  if(digitalRead(6) == LOW){
    Buttons.pressButton(8);
    delay(100);
    Buttons.releaseButton(8);   
  } 
  digitalWrite(3,HIGH);
  digitalWrite(8,LOW);
  digitalWrite(7,HIGH);
  digitalWrite(5,HIGH);
  if(digitalRead(4) == LOW){
    Buttons.pressButton(3);
    delay(100);
    Buttons.releaseButton(3);   
  }
  if(digitalRead(2) == LOW){
    Buttons.pressButton(4);
    delay(100);
    Buttons.releaseButton(4);   
  }
  if(digitalRead(6) == LOW){
    Buttons.pressButton(5);
    delay(100);
    Buttons.releaseButton(5);   
  }
  digitalWrite(3,LOW);
  digitalWrite(8,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(5,HIGH);
  if(digitalRead(4) == LOW){
    Buttons.pressButton(0);
    delay(100);
    Buttons.releaseButton(0);   
  }
  if(digitalRead(2) == LOW){
    Buttons.pressButton(1);
    delay(100);
    Buttons.releaseButton(1);   
  }
  if(digitalRead(6) == LOW){
    Buttons.pressButton(2);
    delay(100);
    Buttons.releaseButton(2);   
  }
}

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