#Buttons are digital inputs
The buttons on Arduboy are configured as digital inputs and utilize internal pull-up resistors on the micro controller. The resistors place a voltage on one side of the momentary push buttons, and the other side is connected to ground. Once the button is pressed, the input pin is pulled to ground.
##pinMode()
Configuring the pin is accomplished using the function pinMode(). The first variable is the pin we wish to control, and the second variable defines the configuration. We want to define the pin as an input and use the internal pullup so we use:
pinMode(A0, INPUT_PULLUP);
##digitalRead()
To test the state of the buttons we invoke the Arduino function digtalRead(). For example, the A button is configured to the pin A0, so test it’s state we use:
A_button = digitalRead(A0);
If the A button is not pressed, A_button will be 1.
If the A button is pressed, the variable will be 0.
This may seem a little backwards but this is a very common way to configure buttons on a micro controller.
Just curious, aren’t A0 through A5 typically considered analog inputs on Arduino based controllers at least? Yet you claim here buttons are digital inputs but contradict it by using A0 in the example. Well sorry if I’m being a smartass, just wanna know : P
It’s just how the leonardo is setup, almost all the pins are analog. I think you can address them as very high numbers (like 15 or 16 or something— this is totally wrong…) but this just seemed easier at the time.
But yes it’s a little confusing, if you find out other pin designations to use I’m open to changing it
if (digitalRead(9) == 0) { value = value | B00100000; } // left
if (digitalRead(8) == 0) { value = value | B00010000; } // up
if (digitalRead(5) == 0) { value = value | B00001000; } // right
if (digitalRead(10) == 0) { value = value | B00000100; } // down
if (digitalRead(A0) == 0) { value = value | B00000010; } // a?
if (digitalRead(A1) == 0) { value = value | B00000001; } // b?
So in matching with that, I defined the buttons myself like this:
#define LEFT 32
#define UP 16
#define RIGHT 8
#define DOWN 4
#define A_BUTTON 2
#define B_BUTTON 1
And when I check to see if A is pressed in code, the left button is the one that triggers it.
@bateske talked about this a little on IRC a while back. He said that he didn’t necessarily care to stick to Game Boy conventions, and the order A -> B can sort of stand for Ardu -> Boy.
Personally though I would rather have it like the Game Boy just because it’s what I’m used to, and I think what many others are used to. The A button is jump, and jump is on the right side, that sort of thing. I don’t want to tell players to hold A to run and press B to jump.
Just tell them to hit jump and fire and define they how you want in your app. You don’t have to name them A and B in your instructions and I don’t think we’re painting letters on them.
They need to be labeled before the final units ship. I believe I asked @bateske about that a long time ago and he said they would.
It’s not only easier to refer to them by a printed letter, but it uses less characters if you decide to print messages about it on screen. Otherwise people will have to talk about them on forums like “the left button” which can be mistaken for left on the d-pad. People won’t have a universal understanding of “fire,” and besides that many games will not dedicate buttons to a single function at all times.
But yeah, back to the orientation…it’s just my preference, I like B A.