I'm working on a game controller that uses the Arduino Pro Micro with this Joystick library: GitHub - MHeironimus/ArduinoJoystickLibrary: An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.
My controller is planned to have 16 buttons, but since the Pro Micro doesn't really have that many I/O pins, I decided to use the Keypad library (Arduino Playground - Keypad Library) to reduce the pins needed to 8.
My code is below. Everything works fine, expect whenever you press a button, only one input is registered. So if you're holding down a button, the input won't keep repeating like I want it to. How do I make it so the code registers an input when pressing a button and keep doing so until the button is released?
#include <Keypad.h>
#include <Joystick.h>
#define NUM_BUTTONS 16
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
NUM_BUTTONS, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'a', 'b', 'x', 'y'}, //A, B, X, Y
{'u', 'd', 'l', 'r'}, // DPAD UP, DOWN, LEFT, RIGHT
{'+', '-', 'c', 'h'}, // PLUS, MINUS, CAPTURE, HOME
{'1', '2', '3', '4'} // LEFT BUMPER AND TRIGGER, RIGHT BUMPER AND TRIGGER
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
int x1 = A1; //Left Joystick
int y1 = A0;
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
// Initialize Button Pins
Serial.begin(9600);
pinMode(10, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
Joystick.begin();
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);
}
// Last state of the buttons
int lastButtonState[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void checkButtons() {
//Buttons
char key = customKeypad.getKey();
customKeypad.setHoldTime(10);
switch (key) {
case 'a': //A
Joystick.setButton(0, 0);
Serial.println("Button A was pressed");
while (digitalRead(2) == HIGH) {
Joystick.setButton(0, 0);
Serial.println("Button A is held");
}
break;
case 'b': //B
Joystick.pressButton(1);
Serial.println("Button B was pressed");
break;
case 'x': //X
Joystick.pressButton(2);
Serial.println("Button X was pressed");
break;
case 'y': //Y
Joystick.pressButton(3);
Serial.println("Button Y was pressed");
break;
case 'u': //DPAD UP
Joystick.pressButton(4);
Serial.println("DPAD UP was pressed");
break;
case 'd': //DPAD DOWN
Joystick.pressButton(5);
Serial.println("DPAD DOWN was pressed");
break;
case 'l': //DPAD LEFT
Joystick.pressButton(6);
Serial.println("DPAD LEFT was pressed");
break;
case 'r': //DPAD RIGHT
Joystick.pressButton(7);
Serial.println("DPAD RIGHT was pressed");
break;
case '+': //PLUS
Joystick.pressButton(8);
Serial.println("PLUS was pressed");
break;
case '-': //MINUS
Joystick.pressButton(9);
Serial.println("MINUS was pressed");
break;
case 'c': //CAPTURE
Joystick.pressButton(10);
Serial.println("CAPTURE was pressed");
break;
case 'h': //HOME
Joystick.pressButton(11);
Serial.println("HOME was pressed");
break;
case '1': //L.BUMPER
Joystick.pressButton(12);
Serial.println("LEFT BUMPER was pressed");
break;
case '2': //L.TRIGGER
Joystick.pressButton(13);
Serial.println("LEFT TRIGGER was pressed");
break;
case '3': //R.BUMPER
Joystick.pressButton(14);
Serial.println("RIGHT BUMPER was pressed");
break;
case '4': //R.TRIGGER
Joystick.pressButton(15);
Serial.println("RIGHT TRIGGER was pressed");
break;
}
for (int i = 0; i <= 16; i++) {
Joystick.releaseButton(i);
}
Serial.println(customKeypad.getState());
}
void loop() {
//Joystick
int yPosition = analogRead(y1);
int xPosition = analogRead(x1);
if (yPosition >= 0 && yPosition <= 200) {
Serial.println("DOWN on the joystick");
Joystick.setYAxis(1);
} else if (yPosition >= 800) {
Serial.println("UP on the joystick");
Joystick.setYAxis(-1);
} else {
Joystick.setYAxis(0);
}
if (xPosition >= 0 && xPosition <= 200) {
Serial.println("LEFT on the joystick");
Joystick.setXAxis(-1);
} else if (xPosition >= 800) {
Serial.println("RIGHT on the joystick");
Joystick.setXAxis(1);
} else {
Joystick.setXAxis(0);
}
//Buttons
checkButtons();
}