├── README.md └── gamething └── gamething.ino /README.md: -------------------------------------------------------------------------------- 1 | # gamething-controller 2 | Arduino based arcade button encoder 3 | 4 | Installation: 5 | - Copy "gamething" folder into your "Arduino" folder 6 | 7 | More info at: http://www.cuddleburrito.com/blog/2015/3/17/arduino-arcade-joystick-controller -------------------------------------------------------------------------------- /gamething/gamething.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Pro Micro Test Code 4 | by: Florian Maurer 5 | GameThing by Cuddleburrito 6 | date: March 6, 2015 7 | license: Public Domain - please use this code however you'd like. 8 | It's provided as a learning tool. 9 | 10 | This code is provided to how to turn an arduino into an HID keyboard controller for arcade games 11 | */ 12 | 13 | //--------- 14 | // USE THESE FOR THE buttonPresets ARRAY BELOW 15 | #define KEY_LEFT_CTRL 0x80 16 | #define KEY_LEFT_SHIFT 0x81 17 | #define KEY_LEFT_ALT 0x82 18 | #define KEY_LEFT_GUI 0x83 19 | #define KEY_RIGHT_CTRL 0x84 20 | #define KEY_RIGHT_SHIFT 0x85 21 | #define KEY_RIGHT_ALT 0x86 22 | #define KEY_RIGHT_GUI 0x87 23 | 24 | #define KEY_UP_ARROW 0xDA 25 | #define KEY_DOWN_ARROW 0xD9 26 | #define KEY_LEFT_ARROW 0xD8 27 | #define KEY_RIGHT_ARROW 0xD7 28 | #define KEY_BACKSPACE 0xB2 29 | #define KEY_TAB 0xB3 30 | #define KEY_RETURN 0xB0 31 | #define KEY_ESC 0xB1 32 | #define KEY_INSERT 0xD1 33 | #define KEY_DELETE 0xD4 34 | #define KEY_PAGE_UP 0xD3 35 | #define KEY_PAGE_DOWN 0xD6 36 | #define KEY_HOME 0xD2 37 | #define KEY_END 0xD5 38 | #define KEY_CAPS_LOCK 0xC1 39 | #define KEY_F1 0xC2 40 | #define KEY_F2 0xC3 41 | #define KEY_F3 0xC4 42 | #define KEY_F4 0xC5 43 | #define KEY_F5 0xC6 44 | #define KEY_F6 0xC7 45 | #define KEY_F7 0xC8 46 | #define KEY_F8 0xC9 47 | #define KEY_F9 0xCA 48 | #define KEY_F10 0xCB 49 | #define KEY_F11 0xCC 50 | #define KEY_F12 0xCD 51 | 52 | //--------- 53 | 54 | #define LED_PIN 13 55 | //#define BOUNCE_LOCK-OUT //activate the alternative debouncing method. This method is a lot more responsive, but does not cancel noise. 56 | 57 | //========== CONFIGURATION SETTINGS ========== 58 | #define BOUNCE_WAIT 10 59 | #define BOUNCE_COUNT 1 60 | 61 | //pins 2-9, 10, 14,15,16, 18-21 62 | //for buttons 1-12, up, down, left and right 63 | int buttonPins[] = { 64 | 9, //button 1 65 | 8, //button 2 66 | 7, //button 3 67 | 3, //button 4 68 | 2, //button 5 69 | 4, //button 6 70 | 6, //button 7 71 | 5, //button 8 72 | 18, //button 9 73 | 19, //button 1 back panel 74 | 20, //button 2 back panel 75 | 21, //button 3 back panel 76 | 10, //joystick up 77 | 16, //joystick down 78 | 14, //joystick left 79 | 15 // joystick right 80 | }; 81 | 82 | char buttonPresets[] = { 83 | KEY_LEFT_CTRL, //button 1 84 | KEY_LEFT_ALT, //button 2 85 | ' ', //button 3 86 | KEY_LEFT_SHIFT, //button 4 87 | 'z', //button 5 88 | 'x', //button 6 89 | '7', //button 7 90 | '8', //button 8 91 | '9', //button 9 92 | '1', //button 1 back panel 93 | '5', //button 2 back panel 94 | '2', //button 3 back panel 95 | KEY_UP_ARROW, //joystick up 96 | KEY_DOWN_ARROW, //joystick down 97 | KEY_LEFT_ARROW, //joystick left 98 | KEY_RIGHT_ARROW // joystick right 99 | }; 100 | 101 | //========== END CONFIGURATION SETTINGS ========== 102 | 103 | int RXLED = 17; // The RX LED has a defined Arduino pin 104 | // Instantiate button state array 105 | boolean buttonPressed[16]; 106 | // Instantiate a Bounce object array to store each debouncer in 107 | Bounce debouncers[16]; 108 | //Instantiate a counter array for each button to debounce count timer 109 | int debounceCount[16]; 110 | 111 | void setup() { 112 | // put your setup code here, to run once: 113 | 114 | pinMode(RXLED, OUTPUT); // Set RX LED as an output 115 | // TX LED is set as an output behind the scenes 116 | 117 | //Setup the LED : 118 | pinMode(LED_PIN,OUTPUT); 119 | digitalWrite(RXLED, LOW); 120 | 121 | 122 | Keyboard.begin(); 123 | 124 | // Create debounce instances : 125 | for (int i = 0; i < 16; i++) { 126 | debouncers[i] = Bounce(); 127 | debounceCount[i] = 0; 128 | pinMode(buttonPins[i],INPUT_PULLUP); 129 | (debouncers[i]).attach(buttonPins[i]); 130 | (debouncers[i]).interval(BOUNCE_WAIT); 131 | delay(100); 132 | buttonPressed[i] = false;, 133 | } 134 | 135 | } 136 | 137 | void loop() { 138 | 139 | for (int j = 0; j < 16; j++) { //iterate over each button (pin) 140 | 141 | (debouncers[j]).update(); //check current value 142 | int value = (debouncers[j]).read(); 143 | 144 | if ( value == LOW ) { //if button pressed 145 | 146 | if(debounceCount[j] == BOUNCE_COUNT && buttonPressed[j] == false) { //the button has been held down long enough and it hasn't been previously registered as pressed 147 | Keyboard.press(char(buttonPresets[j])); //Keyboard.write('1'); 148 | buttonPressed[j] = true; 149 | } else { 150 | if(debounceCount[j] < BOUNCE_COUNT) { 151 | debounceCount[j] = debounceCount[j] + 1; //else increment the count 152 | } 153 | } 154 | 155 | } else { //button is not pressed 156 | 157 | if(debounceCount[j] > 0) { 158 | debounceCount[j] = debounceCount[j] - 1; //keep decreasing count unless 0 159 | } else { 160 | Keyboard.release(char(buttonPresets[j])); //if 0 then release button 161 | buttonPressed[j] = false; 162 | } 163 | 164 | } 165 | 166 | } 167 | 168 | } 169 | --------------------------------------------------------------------------------