Skip to content

Instantly share code, notes, and snippets.

@pondahai
Last active June 15, 2019 11:47
Show Gist options
  • Save pondahai/4adf38c9eab4fdfc109cc1a3faf6cde5 to your computer and use it in GitHub Desktop.
Save pondahai/4adf38c9eab4fdfc109cc1a3faf6cde5 to your computer and use it in GitHub Desktop.
// Simple gamepad example that demonstraits how to read five Arduino
// digital pins and map them to the Arduino Joystick library.
//
// The digital pins 2 - 6 are grounded when they are pressed.
// Pin 2 = UP
// Pin 3 = RIGHT
// Pin 4 = DOWN
// Pin 5 = LEFT
// Pin 6 = FIRE
// Pin 7 = FIRE
// Pin 8 = FIRE
// Pin 9 = FIRE
// Pin 10 = FIRE
// Pin 11 = FIRE
// Pin 12 = FIRE
// Pin 13 = FIRE
//
// NOTE: This sketch file is for use with Arduino Leonardo and
// Arduino Micro only.
//
// by Matthew Heironimus
// 2016-11-24
// mod by Dahai Pon
// 2019-4-9 init
// 2019-4-17 modify for 8 keys
// auto fire on/off by key0 press during power on
// 2019-6-15
//--------------------------------------------------------------------
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,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
bool autoFireSwitch = false;
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);
if (digitalRead(6) == LOW) {
autoFireSwitch = true;
}
}
// Last state of the buttons
int lastButtonState[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
int lastAutoButtonState[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
volatile int autoFireCounter = 0;
#define AFCOUNT 8
void loop() {
if(autoFireCounter++ == AFCOUNT) autoFireCounter = 0;
// 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: // FIRE
Joystick.setButton(0, currentButtonState);
break;
case 5: // FIRE
Joystick.setButton(1, currentButtonState);
break;
case 6: // FIRE
Joystick.setButton(2, currentButtonState);
break;
case 7: // FIRE
Joystick.setButton(3, currentButtonState);
break;
case 8: // FIRE
Joystick.setButton(4, currentButtonState);
break;
case 9: // FIRE
Joystick.setButton(5, currentButtonState);
break;
case 10: // FIRE
Joystick.setButton(6, currentButtonState);
break;
case 11: // FIRE
Joystick.setButton(7, currentButtonState);
break;
}
lastButtonState[index] = currentButtonState;
if(lastAutoButtonState[index] == 1 && currentButtonState == 0)
{
switch (index) {
case 7:
Joystick.setButton(0, 0);
break;
case 8:
Joystick.setButton(1, 0);
break;
case 9:
Joystick.setButton(2, 0);
break;
}
}
lastAutoButtonState[index] = currentButtonState;
}
for(int index = 0; index < 12; index++){
if(autoFireSwitch){ // button no change
if(autoFireCounter==0 && lastAutoButtonState[index]){
switch (index) {
case 7:
Joystick.setButton(0, 0);
break;
case 8:
Joystick.setButton(1, 0);
break;
case 9:
Joystick.setButton(2, 0);
break;
}
}
if(autoFireCounter==(AFCOUNT/2) && lastAutoButtonState[index]){
lastAutoButtonState[index] = 1;
switch (index) {
case 7:
Joystick.setButton(0, 1);
break;
case 8:
Joystick.setButton(1, 1);
break;
case 9:
Joystick.setButton(2, 1);
break;
}
}
}
}
}
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment