Trying to use button with Arduino Leonardo

Hi there!
I am wondering if I would be able to use a small micro switch button thing with my Arduino Leonardo. I want it so that when the button is pressed something happens in the game I am playing (like it presses a key or something I don't know how it works). I have no idea where I would solder it or how to code it. Could somebody help me out please?
Thanks!

Hi @hryy. The key is to start out simple and learn how to use the button by itself before you get into trying to integrate it with your game.

This tutorial is a good place to begin:

After that, you will want to learn about debouncing:

Once you have the button working perfectly on its own, you should switch to the keyboard emulation part of the project. You can learn about that in this tutorial:

@hryy, your topic has been moved to a more suitable location on the forum.

Introductory Tutorials is not for questions, but you welcome to write a tutorial once you have mastered your project :wink:

Thanks @pert!
Do I need this 'breadboard' thing for it to work or is it just for testing. Sorry if this is a stupid question I'm really bad at this stuff!
Thanks

Oh I thought this was asking for a tutorial for a new person, not making tutorials for new people, my bad.

A breadboard is just a convenient way to make temporary electrical connections between things while experimenting or prototyping circuits. You are welcome to use any method you like for making the electrical connections between the button and the Leonardo.

I am very bad at this stuff and need to know how to program my button (soldered to the number 9 input thing) so that when it is pressed it presses a key on the keyboard so I can use it in a game. Any help is appreciated.
Thanks.

Hello
take a view to this

Is the resistor necessary?

In that circuit, yes.

You can also wire the switch between the input pin and GND, then use pinMode(pin, INPUT_PULLUP), which connects an internal resistor to the power supply.

configuring the pin an INPUT_PULLUP uses as internal pull-up resistor

here's logic to recognize a change in button state (HIGH to LOW)

    static byte butLst = HIGH;
    byte but = didgitalRead (butPin);
    if (butLst != but)  {
        if (LOW == (butLst = but))
            // button press action
    }

So where in the code do I place the internal resistor and since I am not using an LED do I delete all the stuff about it in the code. Lastly, will it just work in game or do I need to assign it to a key on the keyboard then have that key do the thing in game.
Thanks.

See the tutorial I linked to above.

As I recommended before, get the code working perfectly with the LED before you even think about starting with the Keyboard part.

Yes.

Thank you I will try this now

I've merged your cross-posts @hryyh.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide you will find at the top of every forum category. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.