Simple push button for PC project (joystick). Windows says button always ON?

Hi All,

Having some issues with this at the moment. I have the wiring and button all setup, and it appears to be working, however, in windows, when looking at the joystick preferences to check that the button is working correctly, it is always in an on state, and turns off when i press the button. I need this to be the reverse, and I have tried a pull down resister between switch and gnd, with no difference! In fact it doesn’t matter what I do, I cannot change this behaviour.

I have also tried changing input type from INPUT_PULLUP, to just INPUT, with no resolution. This will not work properly in its current state as windows will see this button as always on!

Any assistance appreciated. I am sure its something simple!

// Simple example application that shows how to read four Arduino
// digital pins and map them to the USB Joystick library.
//
// Ground digital pins 9, 10, 11, and 12 to press the joystick 
// buttons 0, 1, 2, and 3.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

#include <Joystick.h>

Joystick_ Joystick;

void setup() {
  // Initialize Button Pins
  pinMode(2, INPUT);
  pinMode(9, INPUT);
  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[4] = {0,0,0,0};

void loop() {

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

  delay(10);
}

Good start but you used quotes instead of code tags.

My understanding of the Windows API for joysticks and other USB game consoles is that its looking for multi-byte codes, not electrical levels. Look at your mapping a little more closely.

DKWatson:
Good start but you used quotes instead of code tags.

My understanding of the Windows API for joysticks and other USB game consoles is that its looking for multi-byte codes, not electrical levels. Look at your mapping a little more closely.

Updated thank you.

Excuse my newbieness, but are you referring to:

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

If so, isn’t that correct? I want to map pin 9 to the switch I have setup.

No, the mapping that tales place when the Arduino sorts out the pin signals and sends a code up the USB pipe.

This will not work properly in its current state as windows will see this button as always on!

So, change that.

What do you suppose

      Joystick.setButton(index, currentButtonState);

does?

What do you suppose that

      Joystick.setButton(index, !currentButtonState);

would do?

If you don't know, try the change, and learn.