Hello
I am a complete beginner with no programming and Arduino skills. But in the last few days I tried to read a lot of stuff available regarding it to start my journey with it.
As this is my first project, I plans to make a simple USB keyboard/Mame arcade stick which I am going to use for other PC games like Devil May Cry3 as well. So I figured, I need 4 directional keys, 8 action buttons, 2 keys for start & coin, Escape and Enter key ( just like in computer keyboard). So in total,16 keys.
As I found out that Arduino Leonardo can be emulated as USB keyboard and it provides you with 20 digital I/O keys. I did a extensive search on various libraries regarding it, and found somewhat similar code for it as well (which I modified as per my own need).
I already brought Arduino Leonardo chip and arcade control stick and buttons to move forward with my project.
Kindly check if it will be possible for me to use the analog keys as digital by emulating it as USB keyboard for my arcade controller and it should act as 0 delay keyboard encoder.
#**include** <Keyboard.h>
// MAMEish. An array of pin-key pairs.
struct { int pin; int key; } pinsToKeys[] = {
{ 2, KEY_LEFT_ARROW },
{ 3, KEY_UP_ARROW },
{ 4, KEY_RIGHT_ARROW },
{ 5, KEY_DOWN_ARROW },
{ 6, 's' }, // Fire 1
{ 7, 'd' }, // Fire 2
{ 8, 'f' }, // Fire 3
{ 9, 'x' }, // Fire 4
{ 10, 'c' }, // Fire 5
{ 11, 'v' }, // Fire 6
{ 12, 'g' }, // Button 7
{ 13, 'b' } // Button 8
{ A0, '1' } // select
{ A1, '5' } // Coin
{ A2, KEY_ESC } // Back
{ A3, KEY_RETURN } // Confirm Actions/Activate
};
void setup() {
// Set all used pins to handle input from
// arcade buttons.
for (auto const &pinToKey : pinsToKeys) {
pinMode(pinToKey.pin, INPUT_PULLUP);
}
// Initialize keyboard.
Keyboard.begin();
}
void loop() {
// For each pin-key pair, check the state
// of the pin and set the associated key
// state to match.
for (auto const &pinToKey : pinsToKeys) {
if (digitalRead(pinToKey.pin) == LOW) {
Keyboard.press(pinToKey.key);
} else {
Keyboard.release(pinkey.key);
}
}
}