Joystick buttons - programming

Hello everyone,
I'm very new to Arduino, but I decided to start a small project anyway. The hardware part was easy (attachment) - 12 buttons, 8 switches and a key, built on Arduino Micro.
So far I connected 12 buttons directly from ground to pin 1-12, using the internal pullup resistor.
I used the Arduino Joystick Library to make it look like a game device and uploaded JoystickButton.ino example and the 4 buttons work like they should.

I'm unsure about modifying this line completely - no other help than the commented line ...

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;

Other then that I guess I should modify the rest to reflect the amount of buttons/switches, but when I change those numbers, the game device panel starts flickering and some buttons stay pressed (see attachment2)

// Based on example 
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

#include <Joystick.h>

void setup() {
  // Initialize Button Pins
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;

// Last state of the button
int lastButtonState[12] = {0,0,0,0,0,0,0,0,0,0,0,0};

void loop() {

  // Read pin values
  for (int index = 0; index < 12; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
}

Even at this state the 4 buttons (pin 9,10,11,12) still work like they are supposted to.

What am I doing wrong?

And another (optional) question - what can I do to optimize the game device to only have the 20 buttons/switches, no rudder/thruster/axis/hat options. This doesn't bother me much though, functionality needs to be resolved first :wink:

Thank's for any insight!

12button_fail.jpg

It looks like the joystick library assumes that the four buttons will be wired to consecutive pins. In the example sketch, 9, 10, 11 and 12.

It sounds like you've connected switches to those pins as well? I'd try moving the joystick wires to 13, 14,15 and 16 and make this change:

const int pinToButtonMap = 13;

So that'd mean the constant is the starting pin and the LastButtonState parameter would be amount of buttons from the starting point? That'd make sense ... I'll try that and report with the results :wink:

Allright, I tested the theory and it's exactly like that:

const int pinToButtonMap = 1;

This sets the first used pin in line ... strangely, it does not work from pin 0 ... any idea why?

If I set it to 0 and move the buttons to pins 0-11, only buttons 1-11 work, button from pin 0 doesn't do anything.
When I moved buttons to pins 1-12 and set the constant to 1, all works like a charm.
Thanks for the help :wink:
I'll keep it like this for now and will add the switches and LEDs, which still need some soldering.

I've done a bit more testing and I'm starting to think using the joystick.h library isn't the best choice or needs some serious tweaking for my purpose.
I connected button to pins 0-19 one by one and the result is as follows - only pins 1-13 work for buttons, pins 0 (RX), 14-19 (A0-A5) don't.

Since my panel has 20 buttons/switches total, I'm at a stalemate here - any ideas please?

The documentation for the micro says that it has 20 digital pins. I've never used one, though so my input's all theoretical.

Looking at the pins_aurduino.h file for the micro/leonardo It looks like the pin numbers aren't contiguous. Try doing a pinmode and digitalread from A0, rather than assuming it's 14. Better yet, print A0 etc using the serial port to find out what they really are. Make sure you've selected the correct board in the IDE.

According to this article the analog pins are to be called as 14-19.
I'll probably ask the joystick library author himself, he's bound to know where's the catch :slight_smile:

const int pinToButtonMap = 13; to const byte pinToButtonMap = 13;

you save more RAM for that