Simple toogle switch

Hi guys,

I'm brand new in Arduino, so i hope you can help me! :slight_smile:

I would like to create a toogle switch that acts like if i pressed Y within a game, I think i will be using 3 buttons, all toogle switches, each of them should act like if i pressed Y, Q and G.

How can i programme that?

Hope you can help me, i've been crawling the web for answers, but i can't seem to find anything useful on my problem.

Look at the Arduino Leonardo. It can appear to the PC as a keyboard. There are TONS of tutorials for making it push W, A, S, and D for computer games. All you'd need to do in the code would be to change what letter it sent.

My current project uses a toGGle switch. It's wired to a digital pin and to ground. The code uses digitalRead() to see if it's HIGH or LOW (on or off). It really doesn't get much simpler than that.

You could also look into using state change. I haven't yet. But I will be this weekend when I'm home. Seems a lot of folks point to using it and I want to see if it's less code or easier to implement than what I've been doing.

Hope it helps. Good luck!

Oh wow. Just had a look at state change. That's pretty much what I've been doing. lol. Guess I should have looked at that sooner.

DangerToMyself:
You could also look into using state change. I haven't yet. But I will be this weekend when I'm home. Seems a lot of folks point to using it and I want to see if it's less code or easier to implement than what I've been doing.

It's not about being shorter or better, it does something entirely different. With just a digitalRead then you press the button and it reads LOW so you take some action. Then the loop repeats and the button is still down and reading LOW so it does it again. Then the loop repeats and it's still LOW so it does it again.

With the State Change example you learn to react only once to a button right when it becomes pressed. You only react once per press instead of continuously for the whole time it is pressed down.

Delta_G:
It's not about being shorter or better, it does something entirely different. With just a digitalRead then you press the button and it reads LOW so you take some action. Then the loop repeats and the button is still down and reading LOW so it does it again. Then the loop repeats and it's still LOW so it does it again.

With the State Change example you learn to react only once to a button right when it becomes pressed. You only react once per press instead of continuously for the whole time it is pressed down.

I get it. And unbeknownst to me, it's practically what I have been doing. However, with a toggle, I have to check it in the setup() to see where it is at start up. In case of power failure or whatever. Or I could just do an if(HIGH) check in the loop(). But I prefer to check it and set a variable in setup()

I don't see much point in doing a state change test on a toggle. The current debounced state is normally all you need.