Arduboy Buttons: How do they work?

#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.

2 Likes

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 :slight_smile:

1 Like

Creating these tiny tutorials to help everyone get started on Arduboy dev :smile:

1 Like

I think your B and A might be switched.

Arduboy.cpp uses this code:

  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.

A is the left button, B is the right. Alpha order, not matching gameboy.

Yes, I checked that. Shouldn’t it match the Gameboy? Makes it easier for the gamers :smiley:

@bateske: can we make A as right button and B as left button like Gameboy?

@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. :stuck_out_tongue:

1 Like

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.

I think A is left and B is right, because it’s like ArduBoy and also different than the gameboy.

We will probably silkscreen some labels on the PCB of the finished one it’s been a pretty common request!

1 Like

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.

@bateske has said A B is the order because we aren’t a Gameboy, we’re an Arduboy - a different thing.

This image needs to be updated since we are going with A->B now for buttons.

Edit: I think the button letters would look cool if engraved in the button itself . Like this pic of the N64 controller img

Good catch. @bateske can we do anything about the image if the offcial standard is no A -> B.