Page 1 of 1

Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Mon Apr 03, 2023 9:25 am
by thorr

I am putting the finishing touches on my arcade cabinet and just got my coin door wired up along with some other buttons like Start 1P using an Arduino. I programmed it to be a keyboard. The code is the same for all the buttons/key presses.

I can't for the life of me figure out why none of the arcade cores will accept the "Coin". It is sending the "5" key. I tested having it repeat the 5 with no delay and with just sending one 5, and it makes no difference. When I map the buttons, it recognizes the "5" when I insert a coin. If I Press F9 and go to the Login prompt and insert a coin, it will type a 5 (or a few if there are no delays in the programming). But the arcade cores will not get a credit. If I press "5" on a different keyboard, it works fine and gives me a credit. I can then press the Start 1P button on my arcade cabinet that uses the same Arduino as the coin function that isn't working and it will start just fine.

I even changed the order and mapped the coin mechanism to be the Start 1P and the Start 1P button to be the Coin function. In this case, the coin still does not get accepted in the core. Pressing 5 on a different keyboard gives me a credit, then inserting a coin into the mechanism starts the game. So the issue is not with the Arduino per se, but the Coin function in arcade cores when that Arduino device is used. I even tried changing the PID and VID. Didn't help. I also tried deleting all the input map files and starting over. Didn't help. I tried different arcade cores. None of them work. It is so strange.

I even tried it on my second MiSTer and it acted the same way. Any ideas?


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Mon Apr 03, 2023 1:13 pm
by aberu

What's the code for the arduino? My guess would be that you aren't using any debounce.


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Mon Apr 03, 2023 3:50 pm
by thorr
aberu wrote: Mon Apr 03, 2023 1:13 pm

What's the code for the arduino? My guess would be that you aren't using any debounce.

Here is the current version with a delay of 500. Thanks a lot for your help!

#include <Keyboard.h> //Including the keyboard library

//Declaring variables for the pins
int StartA = 2; // Orange
int StartB = 3; // Brown
int StartC = 4; // Gray
int StartD = 5; // Purple
int CoinB = 6; // Yellow
int CoinA = 7; // Blue
int CoinC = 8; // Future
int CoinD = 9; // Future

void setup() {

pinMode(StartA, INPUT_PULLUP); //Setting up the internal pull-ups resistors
pinMode(StartB, INPUT_PULLUP); //and also setting the pins to inputs.
pinMode(StartC, INPUT_PULLUP);
pinMode(StartD, INPUT_PULLUP);
pinMode(CoinA, INPUT_PULLUP);
pinMode(CoinB, INPUT_PULLUP);
pinMode(CoinC, INPUT_PULLUP);
pinMode(CoinD, INPUT_PULLUP);
}

void loop() {

if (digitalRead(StartA) == LOW)
{
Keyboard.write('1'); //Sending the "1" character
// delay(30);
}

if (digitalRead(StartB) == LOW)
{
Keyboard.write('2'); //Sending the "2" character
// delay(30);
}

if (digitalRead(StartC) == LOW)
{
Keyboard.write('3'); //Sending the "3" character
// delay(30);
}

if (digitalRead(StartD) == LOW)
{
Keyboard.write('4'); //Sending the "4" character
// delay(30);
}

if (digitalRead(CoinA) == LOW)
{
Keyboard.write('5'); //Sending the "5" character
delay(500);
}

if (digitalRead(CoinB) == LOW)
{
Keyboard.write('6'); //Sending the "6" character
delay(500);
}

if (digitalRead(CoinC) == LOW)
{
Keyboard.write('7'); //Sending the "7" character
delay(500);
}

if (digitalRead(CoinD) == LOW)
{
Keyboard.write('8'); //Sending the "8" character
delay(500);
}
}


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Mon Apr 03, 2023 5:33 pm
by Flandango

Just in case...did you configure the core(s) button mapping with the controller itself and not just a keyboard? Even if your controller is acting like a keyboard, it still has to mapped to that keyboard/controller's ID.


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Mon Apr 03, 2023 5:46 pm
by thorr
Flandango wrote: Mon Apr 03, 2023 5:33 pm

Just in case...did you configure the core(s) button mapping with the controller itself and not just a keyboard? Even if your controller is acting like a keyboard, it still has to mapped to that keyboard/controller's ID.

Thanks, yes I did. This raises another question though. When I went to map it, the first thing it asks me is to "Press Right". To get around this, I press Start for player 4 to wake it up, since my test core doesn't use that. How do I get it to only use what I have? I am stuck on the "Press Right" screen, and if I use anything else to try to bypass it, I get the wrong pid/vid and it won't accept any other keys. I tried pressing the User button and it didn't help. Eventually, when I have a 4-player core, this workaround won't work. I am currently thinking I need to hook up a dummy button to the Arduino just so I can wake it up to program it. If I choose something like Start for player 1 on the "Press Right" screen, I can't map it when it asks for Start for player 1 because I already used it. I think the input system at a minimum should first ask to press anything so it can determine the vid/pid and then start asking for directions that can be skipped.


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Mon Apr 03, 2023 6:53 pm
by Flandango

Ah ok, I didn't pay close attention. For some reason I thought you had the arduino handle the whole controller.
As far as I know, I don't think you can have a separate "controller" just for the coin/start buttons. I could be wrong but I haven't seen a scenario that allows for it, unless of course, the core was written to look for key presses instead of joystick input.
One possible scenario I can think of is, as you mentioned, add another hidden/dummy button on the arduino that sends another keystroke (ex Right Arrow), then use that on the Press Right option (you can't bypass it with the User button since it uses that opportunity to read the vid/pid). For the other options, you can click the User button to not configure them.
Then of course configure each player's controls (minus the coin/start buttons) and then when you start a core, make sure you move each joystick (or press a button) in the player order you want before pressing the coin/start button of the "coin controller".
Now I don't know how well that works because, while I know most arcade cores have a "Start Player 2 button" on each controller, some may not offer that and expect the "Start" button to be on that player's controller.


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Mon Apr 03, 2023 7:22 pm
by thorr

Yikes! Thanks for the details. I started a thread a few days ago in the Input section about similar limitations. (Edit: Oops, I forgot you already responded there. Thanks!) I really hope someday the input system can be completely replaced to one like the Mame input system where any function on any device can be mapped to any core input for any player. Meanwhile, I hope I can get this non-working coin function sorted out. It is very strange that the start buttons work, but the coin function doesn't, in any core.


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Tue Apr 04, 2023 12:53 am
by thorr

I did some digging while at work and I am going to try using the keyboard.press and keyboard.release functions instead of keyboard.write. I hope this fixes the issue. I was wondering how that works when holding multiple keys anyway, so most likely this will help (I hope).


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Tue Apr 04, 2023 3:56 am
by thorr

Solved! I changed the coin code to this and it works!

if (digitalRead(CoinA) == LOW) //Checking if the first switch has been pressed
{
Keyboard.press('5');
delay(100);
Keyboard.release('5');
}

I will most likely clean up this code to make it nice, fast and intelligent, but for start buttons and coin slots, it is already good enough. Thanks everyone for your input and help!


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Tue Apr 04, 2023 4:52 am
by jca

I think you should debounce the buttons and send the release only after the button has been released (after debouncing).


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Tue Apr 04, 2023 4:55 am
by thorr

Yep, thanks! This was just a temporary test to see if it would work at all. I already rewrote the code, played a game of Centipede using a quarter and a start button, and got my second highest score so far. :D Here is the final version:

#include <Keyboard.h> //Including the keyboard library

//Declaring variables for the pins
int StartA = 2; // Orange
int StartB = 3; // Brown
int StartC = 4; // Gray
int StartD = 5; // Purple
int CoinB = 6; // Yellow
int CoinA = 7; // Blue
int CoinC = 8; // Future
int CoinD = 9; // Future
bool PrStartA = 0; // Last state of button press
bool PrStartB = 0;
bool PrStartC = 0;
bool PrStartD = 0;
bool PrCoinA = 0;
bool PrCoinB = 0;
bool PrCoinC = 0;
bool PrCoinD = 0;

void setup() {

pinMode(StartA, INPUT_PULLUP); //Setting up the internal pull-ups resistors
pinMode(StartB, INPUT_PULLUP); //and also setting the pins to inputs.
pinMode(StartC, INPUT_PULLUP);
pinMode(StartD, INPUT_PULLUP);
pinMode(CoinA, INPUT_PULLUP);
pinMode(CoinB, INPUT_PULLUP);
pinMode(CoinC, INPUT_PULLUP);
pinMode(CoinD, INPUT_PULLUP);
}

void loop() {

if (digitalRead(StartA) == LOW) //If the button is currently pressed
{
if (PrStartA == 0) // Will be 0 one time at the beginning of the button press
{
PrStartA = 1; // The button is pressed until it isn't
Keyboard.press('1'); // Send the keyboard press signal one time until it is eventually released
delay(3); // Prevent triple typing on first press
}
} else // The button is not currently pressed
{
if (PrStartA == 1) // Will be 1 one time at the end of the button press
{
PrStartA = 0; // The button is released until it is eventually pressed again
Keyboard.release('1'); // Send the keyboard release signal one time until it is eventually pressed again
}
}

if (digitalRead(StartB) == LOW) //If the button is currently pressed
{
if (PrStartB == 0) // Will be 0 one time at the beginning of the button press
{
PrStartB = 1; // The button is pressed until it isn't
Keyboard.press('2'); // Send the keyboard press signal one time until it is eventually released
delay(3); // Prevent triple typing on first press
}
} else // The button is not currently pressed
{
if (PrStartB == 1) // Will be 1 one time at the end of the button press
{
PrStartB = 0; // The button is released until it is eventually pressed again
Keyboard.release('2'); // Send the keyboard release signal one time until it is eventually pressed again
}
}

if (digitalRead(StartC) == LOW) //If the button is currently pressed
{
if (PrStartC == 0) // Will be 0 one time at the beginning of the button press
{
PrStartC = 1; // The button is pressed until it isn't
Keyboard.press('3'); // Send the keyboard press signal one time until it is eventually released
delay(3); // Prevent triple typing on first press
}
} else // The button is not currently pressed
{
if (PrStartC == 1) // Will be 1 one time at the end of the button press
{
PrStartC = 0; // The button is released until it is eventually pressed again
Keyboard.release('3'); // Send the keyboard release signal one time until it is eventually pressed again
}
}

if (digitalRead(StartD) == LOW) //If the button is currently pressed
{
if (PrStartD == 0) // Will be 0 one time at the beginning of the button press
{
PrStartD = 1; // The button is pressed until it isn't
Keyboard.press('4'); // Send the keyboard press signal one time until it is eventually released
delay(3); // Prevent triple typing on first press
}
} else // The button is not currently pressed
{
if (PrStartD == 1) // Will be 1 one time at the end of the button press
{
PrStartD = 0; // The button is released until it is eventually pressed again
Keyboard.release('4'); // Send the keyboard release signal one time until it is eventually pressed again
}
}

if (digitalRead(CoinA) == LOW) //If the button is currently pressed
{
if (PrCoinA == 0) // Will be 0 one time at the beginning of the button press
{
PrCoinA = 1; // The button is pressed until it isn't
Keyboard.press('5'); // Send the keyboard press signal one time until it is eventually released
delay(3); // Prevent triple typing on first press
}
} else // The button is not currently pressed
{
if (PrCoinA == 1) // Will be 1 one time at the end of the button press
{
PrCoinA = 0; // The button is released until it is eventually pressed again
Keyboard.release('5'); // Send the keyboard release signal one time until it is eventually pressed again
}
}

if (digitalRead(CoinB) == LOW) //If the button is currently pressed
{
if (PrCoinB == 0) // Will be 0 one time at the beginning of the button press
{
PrCoinB = 1; // The button is pressed until it isn't
Keyboard.press('6'); // Send the keyboard press signal one time until it is eventually released
delay(3); // Prevent triple typing on first press
}
} else // The button is not currently pressed
{
if (PrCoinB == 1) // Will be 1 one time at the end of the button press
{
PrCoinB = 0; // The button is released until it is eventually pressed again
Keyboard.release('6'); // Send the keyboard release signal one time until it is eventually pressed again
}
}

if (digitalRead(CoinC) == LOW) //If the button is currently pressed
{
if (PrCoinC == 0) // Will be 0 one time at the beginning of the button press
{
PrCoinC = 1; // The button is pressed until it isn't
Keyboard.press('7'); // Send the keyboard press signal one time until it is eventually released
delay(3); // Prevent triple typing on first press
}
} else // The button is not currently pressed
{
if (PrCoinC == 1) // Will be 1 one time at the end of the button press
{
PrCoinC = 0; // The button is released until it is eventually pressed again
Keyboard.release('7'); // Send the keyboard release signal one time until it is eventually pressed again
}
}

if (digitalRead(CoinD) == LOW) //If the button is currently pressed
{
if (PrCoinD == 0) // Will be 0 one time at the beginning of the button press
{
PrCoinD = 1; // The button is pressed until it isn't
Keyboard.press('8'); // Send the keyboard press signal one time until it is eventually released
delay(3); // Prevent triple typing on first press
}
} else // The button is not currently pressed
{
if (PrCoinD == 1) // Will be 1 one time at the end of the button press
{
PrCoinD = 0; // The button is released until it is eventually pressed again
Keyboard.release('8'); // Send the keyboard release signal one time until it is eventually pressed again
}
}
}


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Tue Apr 04, 2023 8:59 am
by Robgus

OT I know, but I'm curious to see the cabinet!


Re: Can't get "Coin" to work in any arcade core, but it is sending a 5

Posted: Tue Apr 04, 2023 5:21 pm
by thorr
Robgus wrote: Tue Apr 04, 2023 8:59 am

OT I know, but I'm curious to see the cabinet!

Thanks! :) It's almost ready, but I need to finish it up and clean up around it so I can take nice photos. Hopefully this weekend. I still need to make more control panels for it, but one is pretty much done at least.