So I made a gamepad out of cardboard and some buttons. I'm using Arduino Leonardo with this code I found:
// 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(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[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(50);
}
Everything works fine. I can use it in games etc. But here is the problem. I wanted to use every input possible(so pins 2-13 and A0-A5 that gives 18 in total). So I looked at this code and added required pinMode lines, changed lastButtonState from 4 to 18, added needed "0" in the same line and changed index from 4 to 18 as well. Here comes my problem. All of the inputs flicker in a random pattern when not pressed(even if nothing is connected to the board!) and only couple of them holds their state when pressed. Everything is connected properly and buttons were checked with multimeter.