Arduino Micro and button box help

I have Arduino Micro and I want to create a button box that has 10 buttons connected to D2 thru D12 for driving games. I tried different joystick/gamepad libraries but they don't work. How do I create code in Arduino IDE ?

are you sure it's an Arduino Micro?

what code / library did you try? what did not work?

Probably they do work but you did not use them correctly.

Please never say "doesn't/don't work". It is very unhelpful to those trying to help you. Always describe what happened when you tried and how that was different from what you expected/wanted.

You type it into the main window of the IDE.

Also you can copy and paste it in, but then you are not really creating it :slight_smile:

If you don't know how to use the IDE, how did you test any libraries at all?

I want the Arduino Micro to show up as game controller in "Printers and Devices" window so that I can assign buttons to commands such as ignition switch to button 1 and engine start to button 2 in driving games. If I right-click on Arduino Micro in "Printers and Devices" window, choose "Game Controller Settings", and choose "Properties", I get the test window where I can press buttons to see which button LED light up. I tried one of joystick libraries and when I press a button, the button LEDs light up in a row.

Please show your code (using code tags - please) and how you wired the buttons.

I tried this code and it worked.

// 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_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);

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

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

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

void loop() {

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

  delay(50);
}


surprise :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.