hello all, iv been using the leonardo as a gamepad controller for an arcade cabinet and its working great on all the digital inputs, pins 2 to 13 which gives me 12 digital inputs, 4 for the D pad and 8 for fire buttons.
im using the joystick library obtained form here:
my working code so far for the 12 inputs is:
// Gamepad that uses digital pins for D pad and 8 fire buttons
//
// The digital pins 2 - 13 are grounded when they are pressed.
// Pin 2 = UP
// Pin 3 = RIGHT
// Pin 4 = DOWN
// Pin 5 = LEFT
// Pin 6 = FIRE1
// Pin 7 = FIRE2
// Pin 8 = FIRE3
// Pin 9 = FIRE4
// Pin 10 = FIRE5
// Pin 11 = FIRE6
// Pin 12 = FIRE7
// Pin 13 = FIRE8
//
// NOTE: This sketch file is for use with Arduino Leonardo and
// Arduino Micro only.
//
//--------------------------------------------------------------------
#include <Joystick.h>
Joystick_ Joystick(0x15, JOYSTICK_TYPE_GAMEPAD,
8, 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
void setup() {
// Initialize Button Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);
}
// Last state of the buttons
int lastButtonState[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
void loop() {
// Read pin values
for (int index = 0; index < 12; index++)
{
int currentButtonState = !digitalRead(index + 2);
if (currentButtonState != lastButtonState[index])
{
switch (index) {
case 0: // UP
if (currentButtonState == 1) {
Joystick.setYAxis(-1);
} else {
Joystick.setYAxis(0);
}
break;
case 1: // RIGHT
if (currentButtonState == 1) {
Joystick.setXAxis(1);
} else {
Joystick.setXAxis(0);
}
break;
case 2: // DOWN
if (currentButtonState == 1) {
Joystick.setYAxis(1);
} else {
Joystick.setYAxis(0);
}
break;
case 3: // LEFT
if (currentButtonState == 1) {
Joystick.setXAxis(-1);
} else {
Joystick.setXAxis(0);
}
break;
case 4: // FIRE1
Joystick.setButton(0, currentButtonState);
break;
case 5: // FIRE2
Joystick.setButton(1, currentButtonState);
break;
case 6: // FIRE3
Joystick.setButton(2, currentButtonState);
break;
case 7: // FIRE4
Joystick.setButton(3, currentButtonState);
break;
case 8: // FIRE5
Joystick.setButton(4, currentButtonState);
break;
case 9: // FIRE6
Joystick.setButton(5, currentButtonState);
break;
case 10: // FIRE7
Joystick.setButton(6, currentButtonState);
break;
case 11: // FIRE8
Joystick.setButton(7, currentButtonState);
break;
}
lastButtonState[index] = currentButtonState;
}
}
delay(10);
}
above works great but i was wondeirng if i could use 4 of the analog pins A0-A3 as additional digital inputs. so i tried this code below but im not getting any success:
// Gamepad that has D pad and 8 fire buttons on digital pins and 4 fire buttons on analog pins
//
//
// The digital pins 2 - 13 are grounded when they are pressed.
// Pin 2 = UP
// Pin 3 = RIGHT
// Pin 4 = DOWN
// Pin 5 = LEFT
// Pin 6 = FIRE1
// Pin 7 = FIRE2
// Pin 8 = FIRE3
// Pin 9 = FIRE4
// Pin 10 = FIRE5
// Pin 11 = FIRE6
// Pin 12 = FIRE7
// Pin 13 = FIRE8
// Pin A0 = FIRE9
// Pin A1 = FIRE10
// Pin A2 = FIRE11
// Pin A3 = FIRE12
//
//
//
//
// NOTE: This sketch file is for use with Arduino Leonardo and
// Arduino Micro only.
//
//--------------------------------------------------------------------
#include <Joystick.h>
Joystick_ Joystick(0x15, JOYSTICK_TYPE_GAMEPAD,
12, 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
void setup() {
// Initialize Button Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);
}
// Last state of the buttons
int lastButtonState[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void loop() {
// Read pin values
for (int index = 0; index < 16; index++)
{
int currentButtonState = !digitalRead(index + 2);
if (currentButtonState != lastButtonState[index])
{
switch (index) {
case 0: // UP
if (currentButtonState == 1) {
Joystick.setYAxis(-1);
} else {
Joystick.setYAxis(0);
}
break;
case 1: // RIGHT
if (currentButtonState == 1) {
Joystick.setXAxis(1);
} else {
Joystick.setXAxis(0);
}
break;
case 2: // DOWN
if (currentButtonState == 1) {
Joystick.setYAxis(1);
} else {
Joystick.setYAxis(0);
}
break;
case 3: // LEFT
if (currentButtonState == 1) {
Joystick.setXAxis(-1);
} else {
Joystick.setXAxis(0);
}
break;
case 4: // FIRE1
Joystick.setButton(0, currentButtonState);
break;
case 5: // FIRE2
Joystick.setButton(1, currentButtonState);
break;
case 6: // FIRE3
Joystick.setButton(2, currentButtonState);
break;
case 7: // FIRE4
Joystick.setButton(3, currentButtonState);
break;
case 8: // FIRE5
Joystick.setButton(4, currentButtonState);
break;
case 9: // FIRE6
Joystick.setButton(5, currentButtonState);
break;
case 10: // FIRE7
Joystick.setButton(6, currentButtonState);
break;
case 11: // FIRE8
Joystick.setButton(7, currentButtonState);
break;
case 12: // FIRE9
Joystick.setButton(8, currentButtonState);
break;
case 13: // FIRE10
Joystick.setButton(9, currentButtonState);
break;
case 14: // FIRE11
Joystick.setButton(10, currentButtonState);
break;
case 15: // FIRE12
Joystick.setButton(11, currentButtonState);
break;
}
lastButtonState[index] = currentButtonState;
}
}
delay(10);
}
was hoping someone could help me with this to point me where iv gone wrong. thanks